Skip to content

Raise From Previous Error

Documentation Index

Fetch the complete documentation index at: https://docs.sourcery.ai/llms.txt

Use this file to discover all available pages before exploring further.

Sourcery refactoring id: raise-from-previous-error

Section titled “Sourcery refactoring id: raise-from-previous-error”

Suggests raising from a previously-raised exception.

x = int(input())
try:
print(1 / x)
except ZeroDivisionError:
raise ValueError("Can't divide by zero")
x = int(input())
try:
print(1 / x)
except ZeroDivisionError as e:
raise ValueError("Can't divide by zero") from e

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.