Last updated: Apr 10, 2024
Reading time·3 min
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.
Here is an example of when the warning is shown.
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.
from
keyword when chaining exceptionsTo resolve the issue, use the from
keyword to explicitly specify the object
from which the exception initially occurred.
try: raise ValueError except Exception as e: raise TypeError from e
After making the change, the warning is no longer shown.
You can use a more readable alias for the exception, it doesn't have to be e
.
try: raise ValueError except Exception as MyException: raise TypeError from MyException
An alternative to explicitly setting the context is to use from None
.
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.
try: raise ValueError except Exception as e: raise TypeError('example') from e
Running the code sample produces the following output.
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
Here is a code sample that doesn't use the from
statement.
try: raise ValueError except Exception as e: raise TypeError('example')
And here is the output that is produced.
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
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.
try: raise OSError except OSError as oe: raise FileExistsError('The file already exists') from oe
The code sample produces the following output.
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.
from None
to disable automatic exception chainingAs previously noted, if you want to silence the Pylint warning and disable
automatic exception chaining, use the from None
syntax.
try: raise OSError except OSError as oe: raise FileExistsError('The file already exists') from None
The code sample produces the following output.
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
I've also written a detailed guide on how to disable Pylint warnings in Python.
You can learn more about the related topics by checking out the following tutorials: