Exception

אורי uri at speedy.net
Tue Sep 24 09:51:10 EDT 2019


https://stackoverflow.com/a/24752607/1412564
אורי
uri at speedy.net


On Tue, Sep 24, 2019 at 4:07 PM ast <none at gmail.com> wrote:

> 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
> --
> https://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list