Exception Chaining in Python
Exception chaining preserves the full story when one exception leads to another — using raise NewException("...") from original_exception keeps a record of the original problem attached to the new one, rather than losing that context. It's especially useful when catching a low-level exception and re-raising it as something more meaningful to the caller, without hiding what actually triggered it underneath.
Catch an exception as usual
Inside an except block, exactly as before.
Raise a new exception with from
raise NewException("...") from original_error links the two together.
The original becomes __cause__
Python attaches it to the new exception, and both appear in the traceback.
This beats a plain raise NewException(...)
Which would otherwise hide exactly what triggered the failure underneath.
Chaining also happens automatically
Without from, whenever a new exception is raised while already handling another one.
def parse_age(text): try: return int(text) except ValueError as e: raise ValueError(f"Invalid age: {text!r}") from e try: parse_age("not a number")except ValueError as e: print(e) print(e.__cause__)Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Python yourself — building beats watching.