Skip to content

Latest commit

 

History

History
41 lines (36 loc) · 1002 Bytes

20221025132705.md

File metadata and controls

41 lines (36 loc) · 1002 Bytes

raise from none

If Python exception chaining gets confusing you can use "raise from None" to suppress the initial exception, keeping the second one. Sometimes you might want this extra detail though. Example:

>>> class MyException(Exception):
...     pass
...
>>> try:
...     1/0
... except ZeroDivisionError:
...     raise
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
ZeroDivisionError: division by zero
>>> try:
...     1/0
... except ZeroDivisionError:
...     raise MyException
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
ZeroDivisionError: division by zero

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
__main__.MyException
>>> try:
...     1/0
... except ZeroDivisionError:
...     raise MyException from None
...
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
__main__.MyException

#exceptions