Cosmic Module
J
Qubits of DPK
March 14, 2026
Core Java
Layman Explanation
You're driving and suddenly a tyre punctures. You don't abandon the car — you handle it: pull over, change the tyre, continue. Exception handling is Java's way of dealing with unexpected problems gracefully without crashing the program.
What is an Exception?
An abnormal condition that disrupts the normal flow of a program.
javascript
QUBITS OF DPK
Exception Hierarchy
javascript
QUBITS OF DPK
try-catch-finally
java
QUBITS OF DPK
Multiple Catch
java
QUBITS OF DPK
throw vs throws
throw — explicitly throw exception
java
QUBITS OF DPK
throws — declare exception (duck it)
java
QUBITS OF DPK
Custom Exception
java
QUBITS OF DPK
try-with-resources (Java 7+)
java
QUBITS OF DPK
Production Use Case
java
QUBITS OF DPK
️ All Traps
java
QUBITS OF DPK
Interview Answer (SDE-2)
"Exception handling prevents program crashes by handling abnormal conditions gracefully. Checked exceptions must be handled at compile time (IOException, SQLException). Unchecked exceptions are runtime failures (NPE, ClassCast). try-catch-finally handles exceptions — finally always runs for cleanup. throw explicitly throws; throws declares that a method may throw. Custom exceptions extend Exception for domain-specific errors. try-with-resources auto-closes resources implementing AutoCloseable. In production Spring Boot, @ControllerAdvice with @ExceptionHandler centralizes exception handling across all endpoints."
Interview Questions & MAANG-Level Answers
Q1. What is the difference between checked and unchecked exceptions?
Checked exceptions: subclasses of Exception (not RuntimeException) — MUST be handled at compile time via try-catch or throws declaration. Examples: IOException, SQLException, FileNotFoundException. Force the caller to acknowledge that something can go wrong. Unchecked exceptions: subclasses of RuntimeException — handling is optional, not enforced by compiler. Examples: NullPointerException, ArrayIndexOutOfBoundsException, ClassCastException. Represent programming errors that should ideally be prevented, not caught.
Q2. What is the exception hierarchy in Java?
javascript
QUBITS OF DPK
Q3. What is the difference between throw and throws?
throw is a statement that actually throws an exception object: throw new IllegalArgumentException("invalid"). throws is a declaration in method signature that warns callers the method might throw a checked exception: void read() throws IOException. throw is used inside method body; throws is in method signature. You can throw unchecked exceptions without declaring throws.
Q4. What is finally and when does it NOT run?
finally always executes after try-catch, regardless of whether an exception occurred or was caught — used for guaranteed cleanup (close connections, release locks). When it does NOT run: (1) System.exit() is called — JVM shuts down immediately. (2) JVM crashes (OOME, kill signal). (3) Infinite loop inside try block. Gotcha: if finally has a return statement, it overrides the try block's return, which is a subtle bug to avoid.
Q5. What is try-with-resources and why was it introduced?
Introduced in Java 7 to automatically close resources (files, connections, streams) that implement AutoCloseable. Before: you had to close in finally with null checks — verbose and error-prone. With try-with-resources:
java
QUBITS OF DPK
Resources are closed in reverse order. Exception during close is suppressed (accessible via getSuppressed()).
Q6. How do you create a custom exception?
Extend Exception for checked, RuntimeException for unchecked. Include message and optional cause:
java
QUBITS OF DPK
In Spring Boot: use @ResponseStatus annotation on custom exceptions for automatic HTTP status mapping.
Q7. What is the order of catch blocks?
More specific (child) exceptions MUST come before more general (parent) ones. If Exception is caught first, all child exceptions are unreachable — compile error. Order: NullPointerException before RuntimeException before Exception. Multi-catch (Java 7+) for same handling: catch (IOException | SQLException e) {}.
Q8. What is the difference between Error and Exception?
Error: serious JVM-level problems that applications shouldn't try to recover from (StackOverflowError, OutOfMemoryError, AssertionError). Catching Errors is strongly discouraged — the JVM is in an unstable state. Exception: recoverable application-level problems — should be caught and handled. Both extend Throwable. In production: if you get StackOverflowError — fix the recursion. OutOfMemoryError — fix the memory leak or increase heap (like your notification service fix!).