Skip to content

Instantly share code, notes, and snippets.

@trag1c
Created August 14, 2023 10:42
Show Gist options
  • Save trag1c/b5ea2129e33d7c54571c178a45a4a78a to your computer and use it in GitHub Desktop.
Save trag1c/b5ea2129e33d7c54571c178a45a4a78a to your computer and use it in GitHub Desktop.
Chaining exceptions with `raise from` in Python
>>> try:
...     int("")
... except ValueError:
...     raise RuntimeError

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
RuntimeError
>>> try:
...     int("")
... except ValueError as e:
...     raise RuntimeError from e

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
RuntimeError
>>> try:
...     int("")
... except ValueError:
...     raise RuntimeError from None

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
RuntimeError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment