nextRound
TechnologiesCoding ProblemsBookmarksLearning PathsLogin
nextRound
TechnologiesCoding ProblemsBookmarksLearning PathsLogin
Questions
7 of 8
1What does exception chaining (raise X from Y) accomplish, and how does it aid debugging?
2Your program is consuming increasing memory over time (a suspected memory leak) despite Python's garbage collector. How would you investigate this?
3What is the difference between except Exception and a bare except:? Why is the bare form discouraged?
4What is the purpose of the else and finally clauses in a try block?
5A production script fails intermittently with a RecursionError. How would you diagnose and fix it?
6What tools would you use to profile a slow Python function, and how do you interpret the output?
7How would you design a custom exception hierarchy for an application?
8How would you use Python's logging module effectively instead of print statements for debugging production code?
PythonPython
Basics
Control Flow and Functions
Data Structures
Comprehensions & Functional Programming
Iterators, Generators & Decorators
Object-Oriented Programming
Exception Handling & Debugging
Concurrency & Parallelism
Performance & Optimization
Testing
Security
Modules, Packaging & Environment
Type Hinting & Modern Python
System Design & Architecture with Python
Best Practices & Design Patterns
Edge Cases & Tricky Interview Questions
07 / 08
nextRound

AI-powered interview preparation platform. Practice with curated questions, mock interviews, and personalized learning paths to crack your dream tech interview.

Quick Links

  • Technologies
  • Mock Interviews
  • Saved Questions
  • Pricing

Company

  • About Us
  • Contact Us

Legal

  • Privacy Policy
  • Terms of Use

© 2026 nextRound. All rights reserved.

How would you design a custom exception hierarchy for an application?

Difficulty: 6/10
Custom Exceptions, Exception Hierarchy, Error Contracts

One base app exception plus specific subclasses, carrying structured context

Design an application exception tree with a single root, for example AppError, that subclasses Exception. Under it, group by domain or layer, such as ValidationError, NotFoundError, and ExternalServiceError. Each concrete error carries structured context as keyword arguments so logs and API responses can be built without string parsing. The root class is what callers can catch as a boundary; specific classes are what internal code raises and handles. That gives you a stable contract: the boundary catches AppError and maps it to a status code, while internal code catches the narrow type it can actually recover from. Do not inherit from Exception directly in dozens of unrelated places, and do not swallow the original exception; preserve it with raise ... from ... so the cause chain remains intact.

javascript
  1. 1

    One root class per package or service; internal code raises specific subclasses.

  2. 2

    Carry structured fields in init, such as code, resource_id, retryable, and details.

  3. 3

    Keep the hierarchy shallow; three levels is usually enough. Deep hierarchies make catching ambiguous.

  4. 4

    Map to transport at the boundary, not at the raise site. Let the web or gRPC layer translate AppError subclasses to status codes.

  5. 5

    Common mistake: raising Exception or a generic string, which forces callers into string matching and hides recoverable categories.

  6. 6

    Common mistake: catching a broad AppError and re-raising a different type without raise ... from ..., which loses the original traceback.

  7. 7

    Version note: exception groups (PEP 654) arrived in 3.11 and add an except* syntax for handling multiple concurrent failures, which is worth using in concurrent code.

Scenario Questions

0-2 years experience

  1. 1Why is raising Exception('bad input') worse than raising ValidationError('bad input')?
  2. 2What base class should all your app exceptions share?

2-5 years experience

  1. 1You need an error that carries a resource id and a retryable flag. How do you structure it?
  2. 2Your API layer converts every exception into a 500. How do you map AppError subclasses to proper status codes?

5-8 years experience

  1. 1You have three services and want consistent error semantics. How do you design a shared exception contract without coupling them?
  2. 2Your library wraps a database error in a custom error but loses the SQL error details. How do you preserve the cause and still present a clean API?

8+ years experience

  1. 1Design a cross-service error taxonomy that supports retries, circuit breaking, structured logging, and API error responses without leaking internal details.
  2. 2How do you evolve an exception hierarchy across major versions without breaking callers who catch specific classes?

Follow-up Questions

  • How would you design a retryable versus non-retryable distinction in your hierarchy?
  • How would you use exception groups for concurrent operations?
Sharethis question

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.