Python & FastAPI•Part 2: Python Core Language, Internals & Runtime (6 - 25)
Exception Chaining (`raise ... from ...`):
In Simple Words:
Preserves the root cause exception trace when re-raising a higher-level custom domain exception using from e.
python
class DatabaseError(Exception): pass
def query_user(user_id):
try:
raise ConnectionRefusedError("Connection failed")
except ConnectionRefusedError as err:
# Chains exceptions preserving original traceback:
raise DatabaseError(f"Could not fetch user {user_id}") from err