18 November 2025

#Exception-Handling

#Exception_Handling

Key Concepts


Topic Sub-Topic Basic Intermediate Advanced Expert
Exception Basics Exception, Error vs Exception, Exception hierarchy, Checked vs Unchecked
Try-Catch-Finally try block, catch block, multiple catch, finally block, nested try-catch
Throw & Throws throw keyword, throws keyword, difference, use cases
Custom Exceptions User-defined exceptions, Extending Exception class, Extending RuntimeException class
Exception Propagation Call stack propagation, rethrowing exceptions, handling vs declaring
Multi-Catch & Re-throw Multi-catch (Java 7+), Rethrowing exceptions, Exception wrapping
Try-with-Resources AutoCloseable, Suppressed exceptions, Resource management
Best Practices Checked vs unchecked choice, meaningful messages, logging, avoiding empty catch
Exception Hierarchy Throwable, Error, Exception, RuntimeException, Custom hierarchy
Overriding & Exceptions Exception rules in overriding, narrower exceptions, throws clause differences
Performance Impacts Cost of exceptions, Alternatives to exceptions, Avoiding overuse
Streams Stream pipelines, lambda expressions, handling checked exceptions in lambdas
Framework-Specific Spring @ExceptionHandler, REST Exception Mappers, JUnit expected exceptions
Advanced Scenarios Exception translation, Exception chaining, Domain-specific exceptions, Retry strategies
System-Level Exceptions JVM errors, OutOfMemoryError, StackOverflowError, Fatal exceptions
Design Patterns & Exceptions Try-catch-finally pattern, Command pattern with exceptions, Checked-to-unchecked conversion
Logging & Monitoring Logging frameworks (SLF4J/Log4j), Correlation IDs, Centralized monitoring
Distributed Systems Exception handling across services, retries, circuit breakers, fallback strategies
Expert Practices Fail-fast vs fail-safe, Error boundaries, Designing exception-safe APIs

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