Python raise from usage
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Python's raise ... from ... syntax lets you create an exception chain on purpose. It is useful when one low-level error happens, but you want to raise a higher-level exception that better matches your application's domain without losing the original cause.
Use raise ... from ... to Preserve the Real Cause
Without from, you can catch one exception and raise another, but the relationship between them is less explicit. With from, Python records the original exception as the direct cause.
When this fails, the traceback shows both exceptions and clearly states that the ConfigError was caused by the original ValueError. That is much easier to debug than a generic replacement exception with no chain.
In other words, you are not just raising a new error. You are documenting the causal path from the low-level failure to the higher-level failure that the rest of the application should understand.
Why This Is Better Than Losing Context
Suppose your code reads JSON, validates a field, and exposes a domain-specific API. You usually do not want raw parsing errors to leak directly to every caller, but you also do not want to erase the original failure.
Now callers see an error name that matches your application, while developers still get the low-level parsing cause in the traceback.
This pattern is especially good at module boundaries:
- parsing and validation layers
- database wrappers
- HTTP client abstractions
- library APIs that hide implementation details
Use from None When You Intentionally Want to Hide Context
Sometimes the original exception is just noise for the caller. Python lets you suppress implicit chaining with from None.
This produces a cleaner traceback by omitting the original KeyError as the visible cause. Use it sparingly. Most of the time, preserving the underlying exception is more helpful.
When to Reach for It
Use raise ... from ... when you are translating errors across abstraction layers. A storage layer might catch OSError and raise RepositoryError. A config loader might catch JSONDecodeError and raise SettingsError. The rule is simple: add domain meaning without discarding diagnostic value.
Avoid using it just to wrap every exception automatically. If the higher-level exception adds no useful information, the extra layer may only make the traceback noisier.
That balance is what makes exception chaining valuable. A good chained exception gives the caller a cleaner API while still giving maintainers the original technical reason the operation failed.
Common Pitfalls
- Raising a new exception without
fromand accidentally hiding the original cause. - Over-wrapping exceptions when the new error message adds no meaningful context.
- Using
from Nonetoo aggressively and making debugging harder than it needs to be. - Catching overly broad exceptions such as
Exceptionand then chaining them all into one generic error type.
Summary
- '
raise ... from ...links a new exception to the original cause explicitly.' - It is ideal for translating low-level failures into domain-specific errors.
- The traceback stays informative because Python preserves the cause chain.
- '
from Nonesuppresses the original context when you intentionally want a cleaner error.' - Use exception chaining to improve clarity, not just to add another layer of abstraction.
Related reading
- Python Ramer-Douglas-Peucker RDP algorithm with number of points instead of epsilon
- Python recursive folder read
- Python, remove all non-alphabet chars from string
- python .replace regex
- Python Requests - No connection adapters
- Python Requests throwing SSLError
- Python Request Post with param data
- Python requests - print entire http request raw?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.