Exception

David Raymond David.Raymond at tomtom.com
Tue Sep 24 09:34:36 EDT 2019


I believe the idea is that previously, if you were handling an exception, and your handling code caused its own exception, then you would get no info about the original exception. So all useful information about the original problem would get lost because of an oopsie in the handling code. So it got changed to show both the original exception as well as the final one to be more useful/helpful.

The raise ... from None is basically you explicitly saying "I am <intentionally> raising a different exception, so don't worry about the old one"

Or such is my limited understanding anyway.


-----Original Message-----
From: Python-list <python-list-bounces+david.raymond=tomtom.com at python.org> On Behalf Of ast
Sent: Tuesday, September 24, 2019 9:04 AM
To: python-list at python.org
Subject: Exception

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