Error-handling design
P31.error-handling.01 · Audience: Guest, IT/ML learner, Language professional
Where next?
Later in Error Handling
Go up a level
P31.error-handling.01 · Audience: Guest, IT/ML learner, Language professional
Later in Error Handling
Go up a level
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 (easier to ask forgiveness
than permission: try the operation and handle the exception), and chaining that keeps the
original cause instead of hiding it.
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 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.
Off by default. The mentor always gets your file; this only decides whether your own copy stays here. Copies live in this browser only - they do not follow you to another device, and clearing site data removes them.
Design an exception hierarchy, then translate low-level errors into domain ones with chaining.
Loading exercise…
Loading exercise…
Designing the failure is half the job; the next module is about where you enforce it — the door.
Loading your notes...