01 / 08

What does exception chaining (raise X from Y) accomplish, and how does it aid debugging?

Difficulty: 8/10
Exception Chaining, Cause, Traceback

raise ... from ... sets cause and shows the full traceback chain

When you raise a new exception while handling another, Python automatically sets the new exception's context to the original, and both tracebacks appear with a 'During handling of the above exception, another exception occurred' message. That is implicit chaining. Explicit chaining with raise X from Y sets cause to Y and replaces the message with 'The above exception was the direct cause of the following exception'. Use from when the new exception is a deliberate translation of the original, and from None when you genuinely want to suppress the context, for example to hide an internal error detail at an API boundary. The cause chain is what makes production debugging tractable: the top-level error tells you what failed, and the chain tells you why.

javascript
  1. 1

    context is set automatically when raising inside an except block; cause is set explicitly with raise ... from ....

  2. 2

    raise X from Y makes the translation explicit and keeps Y visible in the traceback.

  3. 3

    raise X from None suppresses the implicit context. Use this sparingly and document why.

  4. 4

    Always log with exc_info=True or log the full formatted exception; a bare message loses the chain.

  5. 5

    Trade-off: leaking internal exceptions through public APIs can expose sensitive details. Translate at the boundary and use from None only when required.

  6. 6

    Common mistake: raising a new exception inside except without preserving the original, which makes debugging a guessing game.

  7. 7

    Version note: exception chaining and cause/context were introduced in PEP 3134 (Python 3.0).

Scenario Questions

0-2 years experience

  1. 1You raise a custom error inside an except and the original error disappears from the logs. Why?
  2. 2What does raise NewError() from OriginalError() change in the traceback?

2-5 years experience

  1. 1You want your public API to return a clean ValidationError while keeping the JSON parse error visible in logs. How do you structure the raise?
  2. 2Your logs show a Python stack trace but no application error because someone used from None. How do you decide whether that was correct?

5-8 years experience

  1. 1You are wrapping a third-party SDK and want to present a stable error surface. How do you translate its exceptions while keeping the cause?
  2. 2You need to redact sensitive fields from a chained exception before it reaches a public log. How do you do this safely?

8+ years experience

  1. 1Design an error-translation layer for a multi-tier service that preserves causes for operators while presenting minimal, redacted messages to clients.
  2. 2Explain how exception chaining interacts with exception groups, async tasks, and structured logging pipelines in Python 3.11+.

Follow-up Questions

  • What is the difference between __cause__ and __context__?
  • When is it appropriate to use raise X from None?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.