Exception

ast none at gmail.com
Tue Sep 24 09:03:35 EDT 2019


Hi

It is not clear to me why the following code
generates 2 exceptions, ZeroDivisionError and
ArithmeticError. Since ZeroDivisionError is
catched, it shoud not bubble out.

Found here:
https://www.pythonsheets.com/notes/python-new-py3.html

 >>> def func():
...     try:
...         1 / 0
...     except ZeroDivisionError:
...         raise ArithmeticError
...
 >>> func()
Traceback (most recent call last):
   File "<stdin>", line 3, in func
ZeroDivisionError: division by zero

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "<stdin>", line 5, in func
ArithmeticError


There is a work around (python >= 3.3)

 >>> def func():
...     try:
...         1 / 0
...     except ZeroDivisionError:
...         raise ArithmeticError from None
...
 >>> func()
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "<stdin>", line 5, in func
ArithmeticError



More information about the Python-list mailing list