Consider explicitly re-raising using 'raise Error from'

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
3 min

banner

# Consider explicitly re-raising using 'raise Error from'

The Pylint warning "Consider explicitly re-raising using 'raise Error from'" is shown when you forget to set the context when re-raising an exception.

To resolve the issue, use the from keyword to specify from which exception the current error is re-raised.

consider explicitly re raising using the from keyword

Here is an example of when the warning is shown.

main.py
try: raise ValueError except Exception as e: # Consider explicitly re-raising using 'raise TypeError from e 'pylint(raise-missing-from) raise TypeError

The code sample uses a try/except statement.

We raised an exception in the try block and caught the exception in the except block.

We directly re-raise a TypeError in the except block, so Pylint shows the warning.

# Use the from keyword when chaining exceptions

To resolve the issue, use the from keyword to explicitly specify the object from which the exception initially occurred.

main.py
try: raise ValueError except Exception as e: raise TypeError from e
The code for this article is available on GitHub

After making the change, the warning is no longer shown.

warning no longer shown

You can use a more readable alias for the exception, it doesn't have to be e.

main.py
try: raise ValueError except Exception as MyException: raise TypeError from MyException

An alternative to explicitly setting the context is to use from None.

main.py
try: raise ValueError except Exception as e: raise TypeError from None

The code sample silences the warning using from None.

Most of the time this isn't necessary as explicitly setting the context is more likely to provide you with more readable error messages.

The context is used to track where an exception was raised and whether another exception was replaced by it.

As shown in this section of the docs, if an unhandled exception occurs in an except block, it will have the exception that is handled attached to it and included in the error message.

Here is an example.

main.py
try: raise ValueError except Exception as e: raise TypeError('example') from e
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
Traceback (most recent call last): File "/home/borislav/Desktop/bobbyhadz_python/main.py", line 2, in <module> raise ValueError ValueError The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/home/borislav/Desktop/bobbyhadz_python/main.py", line 4, in <module> raise TypeError('example') from e TypeError: example

exception chaining

Here is a code sample that doesn't use the from statement.

main.py
try: raise ValueError except Exception as e: raise TypeError('example')

And here is the output that is produced.

shell
Traceback (most recent call last): File "/home/borislav/Desktop/bobbyhadz_python/main.py", line 2, in <module> raise ValueError ValueError During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/borislav/Desktop/bobbyhadz_python/main.py", line 4, in <module> raise TypeError('example') TypeError: example

exception chaining without from statement

The from statement is used to indicate that an exception is a direct consequence of another exception.

The statement is also used when you want to transform an exception into a more specific exception.

main.py
try: raise OSError except OSError as oe: raise FileExistsError('The file already exists') from oe
The code for this article is available on GitHub

The code sample produces the following output.

shell
Traceback (most recent call last): File "/home/borislav/Desktop/bobbyhadz_python/main.py", line 2, in <module> raise OSError OSError The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/home/borislav/Desktop/bobbyhadz_python/main.py", line 4, in <module> raise FileExistsError('The file already exists') from oe FileExistsError: The file already exists

You can view the exception classes in the Exception hierarchy section of the docs.

When raising an exception, make sure it derives from the BaseException class.

# Using from None to disable automatic exception chaining

As previously noted, if you want to silence the Pylint warning and disable automatic exception chaining, use the from None syntax.

main.py
try: raise OSError except OSError as oe: raise FileExistsError('The file already exists') from None

The code sample produces the following output.

shell
Traceback (most recent call last): File "/home/borislav/Desktop/bobbyhadz_python/main.py", line 4, in <module> raise FileExistsError('The file already exists') from None FileExistsError: The file already exists

disable automatic exception chaining

The code for this article is available on GitHub

I've also written a detailed guide on how to disable Pylint warnings in Python.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.