Raise From Previous Error¶
Sourcery refactoring id: raise-from-previous-error
¶
Description:¶
Suggests raising from a previously-raised exception.
Before:¶
x = int(input())
try:
print(1 / x)
except ZeroDivisionError:
raise ValueError("Can't divide by zero")
After:¶
x = int(input())
try:
print(1 / x)
except ZeroDivisionError as e:
raise ValueError("Can't divide by zero") from e
Explanation:¶
Exception chaining was introduced in Python 3
PEP 3134, and is an implicit part
of exception handling. This suggestion converts the implicit exception chain
into an explicit one, as suggested by Pylint's raise-missing-from
error.