Exception handling is essential for building resilient applications and is heavily tested in exam code traces. Master the Throwable hierarchy, execution order, catch ordering rules, and custom exceptions.
In Java, every exception is an object that inherits from java.lang.Throwable. Memorize the exact hierarchy branches:
| Category | Root Parent Class | Compiler Action | Real-World Cause |
|---|---|---|---|
| Error | java.lang.Error |
Not checked by compiler. Cannot handle gracefully. | JVM hardware or memory failure (e.g. infinite recursion causing StackOverflowError). |
| Checked Exception | Direct child of java.lang.Exception |
MANDATORY handling! Compiler refuses to build unless caught (`try-catch`) or declared (`throws`). | External environment failure (file missing on disk, network packet dropped, database connection severed). |
| Unchecked Exception | Child of java.lang.RuntimeException |
Optional handling. Compiler ignores it during compilation. | Developer logic flaws (calling method on null, dividing by 0, bad index array access). |
try-catch-finally Control Flow Execution RulesThink of try as the electrical circuit running normally. If a surge occurs, the circuit trips immediately (jumps out of try). The catch block is the safety breaker that absorbs the surge. The finally block is the emergency backup generatorβit runs no matter what to keep essential security lights on (closing files/connections).
try block completes β finally block runs β code continues below.try block aborts at exception line β matching catch block executes β finally block runs β code continues below.try block aborts β finally block runs β exception bubbles up call stack to crash thread.Catch blocks MUST be ordered from Most Specific (Subclass) to Most General (Superclass). Putting a general exception catch block first causes a COMPILE ERROR (unreachable code)!
finallyIf both try (or catch) and finally contain a return statement, the finally return statement OVERWRITES and MASKS the return value or exception from the try block!
throw vs throws & Custom Exception Authoring| Keyword | Syntax / Location | Purpose | Example |
|---|---|---|---|
throw |
Inside a method body | Explicitly creates and triggers an exception object instance. | throw new IllegalArgumentException("Age cannot be negative"); |
throws |
In method header declaration | Warns callers that this method delegates unchecked/checked exception handling upwards. | public void readFile() throws IOException, SQLException |
To create a custom exception class, inherit from Exception (for checked) or RuntimeException (for unchecked):
When an exception is thrown inside a nested method, Java searches backwards through active stack frames until a matching handler is found:
What is the exact console output of this code snippet?
A inside try.10 / 0 throws ArithmeticException. Statement B is skipped!catch (ArithmeticException) -> Prints C.finally block executes -> Prints D.E.
Method public void process() { throw new IllegalArgumentException(); } has no throws clause and no try-catch. Does it compile?
IllegalArgumentException extends RuntimeException (it is an Unchecked Exception). Unchecked exceptions do NOT require explicit try-catch blocks or throws declarations in method signatures.
Is there any situation in Java where a finally block will NOT execute?
finally is skipped:System.exit(status) before or inside the try block.try or catch block preventing thread from reaching finally.