Python › Exception Handling
What is the difference between 'raise' and 'raise ... from ...' for exception chaining?
IntermediateAnswer
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 entirelyReal-world example Wrapping a low-level parsing error in a more meaningful, higher-level exception while preserving (or intentionally hiding) the original cause.