Advertisement
Intermediate Python, Exception Handling

What is the difference between 'raise' and 'raise ... from ...' for exception chaining?

Short answer

Plain 'raise NewException()' inside an except block still shows BOTH the original and new exception in the traceback (implicit chaining, labeled 'During handling of the above exception...'); 'raise NewException() from original_exception' makes the chaining EXPLICIT, and 'raise…

Save Python mock interview
Advertisement
AModel answer
Plain 'raise NewException()' inside an except block still shows BOTH the original and new exception in the traceback (implicit chaining, labeled 'During handling of the above exception...'); 'raise NewException() from original_exception' makes the chaining EXPLICIT, and 'raise NewException() from None' SUPPRESSES the original exception's context entirely, showing only the new one.
try:
    int("not a number")
except ValueError as e:
    raise RuntimeError("Failed to parse config") from e  # explicit chaining: shows both, labeled 'direct cause'

try:
    int("not a number")
except ValueError:
    raise RuntimeError("Failed to parse config") from None  # suppresses the ValueError entirely
Real-world example
Wrapping a low-level parsing error in a more meaningful, higher-level exception while preserving (or intentionally hiding) the original cause.
Likely follow-ups
  • When would you deliberately want to suppress the original exception's context using 'from None'?
Related topics