Software Engineer (SDE)pythonintermediate3 interactive MCQs in app
Custom Exception Classes
subclass Exception not BaseExceptionshared package base hierarchy & metadatachaining tracebacks with raise ... from err
Core Concept
Custom exceptions subclass `Exception` (NEVER `BaseException`, which catches `SystemExit`/`KeyboardInterrupt`) and carry domain-meaningful names (`InsufficientEvidenceError`, `RateLimitError`) and structured attributes (`status_code`, `retry_after`). Defining one package base exception lets callers catch all domain errors with `except DomainError:` while handling specific subclasses finely.
Worked Example
`class GradingError(Exception): ...` with `ProviderTimeoutError(GradingError)` raised via `raise ProviderTimeoutError(...) from exc` lets routes return 502 while preserving the root HTTP traceback!
Common Pitfall
catching generic `ValueError` everywhere loses distinction between validation bugs vs external rate limits, or forgetting `raise CustomError(...) from err` drops the original traceback cause.
Recommended Readings & Practice
Practice Custom Exception Classes with 3 graded MCQs & written feedback.
Take SOARDO’s free 12-question diagnostic to see how strong you are across your target role and unlock adaptive daily missions.