JAVA — CHAPTER 10

Exception Handling: A Deeper Look · Concept Cheat Sheet
try / catch / finally Checked vs Unchecked Custom Exceptions try-with-resources
1 BASIC try / catch
try { int result = 10 / divisor; } catch (ArithmeticException e) { System.out.println("Can't divide by zero: " + e.getMessage()); }

Why exception handling?

Without a try/catch, an error like division-by-zero or bad input (InputMismatchException) crashes the whole program. Handling it lets the program recover or fail gracefully.

2 THE JAVA EXCEPTION HIERARCHY
Throwable
Exception
Error
RuntimeException
(unchecked)
IOException etc.
(checked)

Everything throwable extends Throwable → splits into Exception (recoverable problems) and Error (serious JVM-level problems, not meant to be caught).

3 CHECKED vs. UNCHECKED EXCEPTIONS

Checked

  • Subclasses of Exception (excluding RuntimeException)
  • Compiler forces you to catch or declare (throws) them
  • e.g. IOException, SQLException

Unchecked

  • Subclasses of RuntimeException
  • Compiler does not require handling — usually programmer errors
  • e.g. NullPointerException, ArithmeticException
4 finally BLOCK & STACK UNWINDING
try { riskyOperation(); } catch (Exception e) { handle(e); } finally { System.out.println("Always runs — cleanup here"); }

Stack unwinding

When an exception isn't caught in the current method, Java "unwinds" back through the call stack, exiting each calling method in turn, until a matching catch is found (or the program terminates).

5 CHAINED & CUSTOM EXCEPTIONS
public class InsufficientFundsException extends Exception { public InsufficientFundsException(String msg) { super(msg); } } // chained: wrap a lower-level cause throw new RuntimeException("Save failed", originalCause);

Why write custom exceptions?

Gives your program's own, meaningful exception types (e.g. InsufficientFundsException) instead of forcing every error into a generic one — makes calling code's catch clauses more precise.

6 PRECONDITIONS, POSTCONDITIONS & ASSERTIONS

Precondition

A condition that must be true before a method runs (e.g. "amount must be positive").

Postcondition

A condition guaranteed true after a method completes successfully.

assert

assert age >= 0 : "age can't be negative";
7 try-WITH-RESOURCES
try (BufferedReader br = new BufferedReader( new FileReader("data.txt"))) { System.out.println(br.readLine()); } // br.close() is called automatically

Why use it?

Any resource implementing AutoCloseable (files, streams, DB connections) is automatically closed when the block ends — even if an exception occurs — eliminating manual finally { close(); } boilerplate.

8 METHOD REFERENCE TABLE — class Throwable / Exception
MethodPurpose
getMessage()returns the exception's detail message
getCause()returns the underlying exception that caused this one (chaining)
printStackTrace()prints the full call-stack trace to System.err
getStackTrace()returns the stack trace as a StackTraceElement[]
toString()class name + message as text
addSuppressed(e)records extra exceptions suppressed during try-with-resources cleanup