Manually raising (throwing) an exception in Python
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Manually raising exceptions is how Python code communicates invalid state, bad inputs, and unsupported operations in a structured way. The raise statement is not only for failures, it is also part of API design and defensive programming. Good exception usage makes debugging faster and error handling predictable.
Raise Built-in Exceptions for Common Errors
Start with built-in exception types whenever they match your scenario.
Choosing a specific exception type matters more than a custom message alone. It lets callers catch meaningful categories instead of broad Exception.
Pick the Right Exception Type
Typical mappings in Python code:
ValueErrorfor invalid value with correct typeTypeErrorfor wrong argument typeKeyErrorfor missing mapping keyRuntimeErrorfor generic runtime state failures
Bad pattern:
Better pattern:
Specific exceptions improve caller behavior and test precision.
Define Custom Exceptions for Domain Errors
When built-ins are too generic, create domain-specific exceptions.
Custom types make service boundaries clearer and reduce fragile string matching in handlers.
Re-Raise While Preserving Original Traceback
If you catch an exception only to log or add context, re-raise correctly.
raise without argument inside except preserves original traceback, which is usually what you want for debugging.
Chain Exceptions for Better Context
Use raise ... from ... when converting low-level exceptions into domain-level ones.
This keeps original cause available while presenting a cleaner interface to higher layers.
You can inspect full chain in traceback, which helps root-cause analysis.
Raise in Validation Layers Early
A practical pattern is fail-fast validation near input boundaries.
Early validation keeps deeper business logic simpler and avoids partial updates with invalid data.
Raise Inside Asynchronous Code
Exception mechanics are same in async functions, but they propagate through await points.
If not caught, async exceptions fail task execution and bubble to event loop error handling.
Testing Raised Exceptions
Unit tests should assert both type and message intent.
This guards behavior and avoids accidental weakening to generic exception types.
Logging and Raise Strategy
A common anti-pattern is logging and re-raising at many layers, producing duplicate logs. Usually log once at boundary where request is handled, then rethrow or convert as needed.
Guideline:
- inner layers raise meaningful exceptions
- boundary layer logs with request context
- avoid swallowing exceptions silently
This keeps logs actionable without noise.
Common Pitfalls
A common pitfall is raising generic Exception everywhere, which makes callers over-catch and hide real bugs. Another is replacing an exception without chaining and losing root cause context. Teams also often catch broad Exception and continue execution in invalid state. Finally, using exceptions for normal control flow in tight loops can hurt readability and performance.
Summary
- Use
raisewith specific exception types. - Prefer built-in exceptions unless domain-specific types add clear value.
- Re-raise with bare
raiseto preserve traceback. - Use
raise ... from ...for explicit causal context. - Keep validation and exception strategy consistent across layers.
Related reading
- Understanding Python super() with __init__() methods
- What is the difference between @staticmethod and @classmethod in Python?
- What is the difference between __str__ and __repr__?
- >, <, >= and <= don''t work with filter in Django
- __init__ got an unexpected keyword argument 'cachedir' when importing top2vec
- __str__ versus __unicode__
- _csv.Error field larger than field limit 131072
- _pickle.PicklingError Could not serialize object TypeError can't pickle _thread.RLock objects
.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.