Error-handling design

P31.error-handling.01 · Audience: guest, it-ml, language-pro

Real LLM grading for this pageLLM grading (this page):

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.

Step 1 / 2A hierarchy you can catch
ⓘ Concept: Errors are a type system too
A domain base class (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.

Ctrl/Cmd + Enter to send

🎓 Practice ladder

2 graded rungs · ~22 min

Design 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?