Error-handling design
P31.error-handling.01 · Audience: guest, it-ml, language-pro
Exceptions are not an afterthought bolted on with a bare except: — they are a
type system for failure. This module designs them: a hierarchy a caller can catch
at the right level, the EAFP style Python prefers, and chaining that keeps the
original cause instead of hiding it.
ⓘ Concept: Errors are a type system too
OrderError) with specific subclasses (OutOfStock, PaymentDeclined) lets one caller write except OrderError for "any order problem" and another except OutOfStock for one case. Design the hierarchy with the same care as your data types.Why it matters — A flat pile of ValueErrors forces callers to string-match messages; a hierarchy lets them catch exactly the level they care about.
Ask the mentor about this module
Ask a question about this content. The mentor explains and grounds its answer in what you are studying; asking is recorded as a learning signal, not a grade.
🎓 Practice ladder
2 graded rungs · ~22 minDesign an exception hierarchy, then translate low-level errors into domain ones with chaining.
Rung 1 — design an exception hierarchy
Loading exercise…
Rung 2 — EAFP and exception chaining
Loading exercise…
Designing the failure is half the job; the next module is about where you enforce it — the door.
Where next?
Later in Error Handling
Go up a level