18 November 2025

#Exception-Handling

#Exception_Handling

Key Concepts


S.No Topic Sub-Topics
1IntroductionDefinition, Errors vs Exceptions, Exception flow, Why needed, Real-world examples
2Exception TypesChecked, Unchecked, Errors, Runtime, Throwable hierarchy
3try-catch BasicsSyntax, Single catch, Exception capture, Debugging, Execution flow
4Multiple catchMultiple catch block, Order, Specific vs Generic, Multi-catch Java 7, Best practices
5finally BlockExecution guarantee, Use cases, Resource cleanup, Try-finally, Finally behavior
6throw KeywordManual throwing, Use cases, Custom messages, Runtime behavior, Stack trace
7throws KeywordMethod signature, Propagation, Checked declaration, Compile errors, Best use
8Exception PropagationCall stack, Propagation rules, try vs throws, Runtime behavior, Examples
9Custom ExceptionsCreate class, Extend Exception, Extend RuntimeException, Fields, Constructors
10Exception Class HierarchyThrowable, Error, Exception, RuntimeException, Libraries
11Common Exception TypesNullPointer, ArrayIndex, Arithmetic, IO, ClassCast
12Stack TracePrint stack trace, Logging stack trace, Trace format, Frames, Debugging
13Best PracticesAvoid swallowing, Custom messages, Single responsibility, Fail fast, Graceful exit
14Checked vs UncheckedDifference, When to use, Design patterns, Pros/Cons, Standards
15Resource ManagementI/O exceptions, Close resources, try-catch-finally, Auto close, Patterns
16Try-with-resourcesJava 7 feature, AutoCloseable, Multiple resources, Suppressed exceptions, Syntax
17Suppressed ExceptionsMeaning, Access suppressed, PrintStackTrace, Use cases, Resource conflict
18Logging BasicsWhy log, Log levels, Log format, Logger libraries, Writing log
19Advanced LoggingSLF4J, Logback, Log4j, Pattern layout, Exception logging
20Exception TranslationMapping exceptions, Wrapping error, Abstraction, custom messages, Conversion
21Global Exception HandlingApplication-level handling, Default handlers, Hooks, Web apps, Spring global handler
22Spring Exception Handling@ExceptionHandler, @ControllerAdvice, Custom response, JSON errors, Logging
23Unit Testing ExceptionsJUnit test, assertThrows, Expected exceptions, Boundary cases, Testing messages
24Debugging TechniquesBreakpoints, Watches, IDE tools, Visual debug, Exception inspect
25Design PatternsCommand pattern exceptions, Builder validation, Strategy fallback, Template hook, Fail-safe
26Performance ConsiderationsCost of exceptions, Avoid overuse, Try/catch slow paths, Control flow bad practice, Benchmarks
27Enterprise HandlingAPI error model, Status codes, Error JSON, Unified error format, Standards
28Exception MetricsMonitoring, Counters, Failure rate, Alerting, Dashboard
29Security & ExceptionsLeaking info, Sanitizing output, Stack trace risk, Error pages, Secure messages
30Interview PrepTop 20 questions, Live coding, Common traps, Custom design, Real project demo

Interview question

Basic Level

  • What is an exception?
  • What is the difference between an error and an exception?
  • What is the difference between checked and unchecked exceptions?
  • What is a try-catch block?
  • What is the purpose of the finally block?
  • How do you throw an exception explicitly?
  • What is the throws keyword?
  • What is the difference between throw and throws?
  • What is a custom exception?
  • How do you create a custom exception?
  • What is the call stack when an exception occurs?
  • How do you print a stack trace?
  • What is exception propagation?
  • What happens if an exception is not caught?
  • What is the default exception handler?
  • What is an InterruptedException and when is it thrown?
  • What is NullPointerException and common causes?
  • What is ArrayIndexOutOfBoundsException?
  • What is ArithmeticException?
  • What is ClassCastException?
  • How do you catch multiple exceptions in a single catch block?
  • What is multi-catch in Java?
  • What is the purpose of Exception class?
  • Can you catch Throwable? Should you?
  • What is the effect of returning from a finally block?

Intermediate Level

  • What is exception chaining (cause)?
  • How do you set the cause of an exception?
  • What is suppression of exceptions?
  • How does try-with-resources work?
  • What are suppressed exceptions in try-with-resources?
  • What is the AutoCloseable interface?
  • How do you design exception messages?
  • What is exception translation/wrapping and why use it?
  • What are best practices for logging exceptions?
  • When should you rethrow an exception?
  • What is a fatal exception vs recoverable exception?
  • How do you implement retry logic safely?
  • What is circuit breaker pattern related to exceptions?
  • How do you handle exceptions in API boundaries?
  • What is error handling strategy in layered architecture?
  • How to avoid swallowing exceptions?
  • What is defensive programming with exceptions?
  • How do you test exception-throwing code?
  • How do you assert exceptions in unit tests?
  • What is the role of exception handling in transactions?
  • How do exceptions affect transactions (rollback)?
  • How to create meaningful exception hierarchies?
  • What are checked exceptions vs unchecked in API design?
  • When to use checked exceptions?
  • When to use unchecked exceptions?

Advanced Level

  • How do exceptions affect performance?
  • What is the cost of throwing exceptions vs checking conditions?
  • How does JVM handle stack traces for exceptions?
  • What is enabling/disabling stack trace generation (Throwable.fillInStackTrace)?
  • What is exception-safe code and strong exception safety guarantees?
  • How to implement circuit breakers with exception patterns?
  • How to design retry/backoff strategies for transient exceptions?
  • How to map exceptions to appropriate HTTP status codes?
  • What is the role of correlation IDs when handling exceptions in distributed systems?
  • How do you implement global exception handlers in web frameworks (e.g., Spring)?
  • How to propagate exceptions across threads and executors?
  • How to capture exceptions from CompletableFuture or async APIs?
  • How to implement exception translation across layers?
  • How do you differentiate transient vs permanent exceptions programmatically?
  • What is the role of idempotency when retrying after exceptions?
  • How to perform fault injection and chaos testing for exception scenarios?
  • How to detect and handle resource exhaustion exceptions?
  • How to design resilient fallbacks for failed operations?
  • What are common anti-patterns in exception handling?
  • How to handle exceptions in streaming or reactive pipelines?
  • How to debug memory leaks caused by exception-related resource leaks?

Expert Level

  • How to design a global error handling strategy for microservices?
  • How to ensure observability of exception events (traces, metrics, logs)?
  • How to design exception taxonomies for large codebases?
  • How to perform exception-driven feature flags and graceful degradation?
  • How to use typed error responses in public APIs while hiding internal details?
  • How to ensure security when exposing exception information?
  • How to correlate user requests with exception traces across services?
  • How to build a centralized exception collection and analysis pipeline?
  • How to use machine learning to detect anomalous exception patterns?
  • How to design for eventual consistency and exception recovery in distributed transactions?
  • How to handle partial failures in distributed systems?
  • What are best practices for exception handling in high-throughput low-latency systems?
  • How to minimize exception overhead in hot code paths?
  • How to use custom exception serializers for cross-language systems?
  • How to implement graceful shutdown with exception cleanup in concurrent services?
  • How to design contract-based error handling (e.g., gRPC status codes)?
  • How to handle exceptions originating from third-party libraries effectively?
  • How to manage breaking changes in exception types across API versions?
  • How to perform post-mortem and RCA for recurring exceptions?
  • How to build automated remediation for known exception patterns?

Bonus: Quick Practicals / Coding Prompts

  • Write code that demonstrates try-with-resources with multiple resources and suppressed exceptions.
  • Implement a custom checked exception and use it in a sample API.
  • Create a utility that converts stack traces to a structured JSON format for logging.
  • Implement retry logic with exponential backoff for a transient IO exception.
  • Demonstrate exception propagation across threads using ExecutorService and Future.
  • Show how to safely close resources even when exceptions are thrown.
  • Implement a global exception handler in a Spring Boot REST API.
  • Write unit tests that assert specific causes of exceptions.
  • Demonstrate wrapping and unwrapping exceptions while preserving the original cause.
  • Implement a simple circuit breaker that trips on repeated exceptions.

Related Topics


   Exception Basics   
   Exception Hierarchy   
   Throw & Throws   
   Custom Exceptions   
   Built-in Exceptions