Exceptions
7 pages · ~4 min✓ Reviewed
Builds on Strings and Immutability. Next up: Generics.
Exception Mental Model
1. Throwable Hierarchy
- Error = JVM failures like
OutOfMemoryError; do not catch - Everything under
ExceptionexceptRuntimeExceptionis checked
2. Checked vs Unchecked
| Checked | Unchecked | |
|---|---|---|
| Extends | Exception, not RuntimeException | RuntimeException or Error |
| Compiler | forces catch or declare | no enforcement |
| Examples | IOException, SQLException | NullPointerException, IllegalArgumentException |
| Use for | recoverable conditions | programming bugs |
3. Common Mistake
- Catching
ThrowableorExceptionswallows Errors and bugs - Catch the most specific type
Rule of thumb
Caller can recover → checked. Caller cannot fix it, it is a bug → unchecked.
try, catch and finally Flow
1. Syntax and Catch Order
tryneeds at least onecatchorfinally- Catch blocks are checked top to bottom: first matching type wins
- Subclass before superclass, or compile error
java
try { read(); } catch (FileNotFoundException e) { } catch (IOException e) { } finally { cleanup(); }
subclass first, superclass after
2. Multi-catch and finally
catch (IOException | SQLException e)handles unrelated typeseis implicitly final- Types cannot be related by inheritance
try-with-resources
1. Syntax and Close Order
- Auto-closes any AutoCloseable declared in
try (...) - Closes in reverse order of declaration
- Closing happens before any
catchorfinally
java
try (BufferedReader br = new BufferedReader(new FileReader(f))) { return br.readLine(); } catch (IOException e) { handle(e); }
- Body
- close()reverse order
- catch
- finally
2. Suppressed and Java 9
- Body and
close()both throw: body exception is primary - Close exception attached via
getSuppressed()
throw vs throws and Custom Exceptions
1. throw vs throws
| throw | throws | |
|---|---|---|
| Is a | statement | signature clause |
| Takes | one exception object | exception types (required for checked) |
| Where | method body | after the parameter list |
| Example | throw new IllegalStateException("msg") | void read() throws IOException |
2. Custom Exceptions
Common Runtime Exceptions
1. Causes and Guards
| Exception | Cause | Guard |
|---|---|---|
NullPointerException | method or field on null | null check, Objects.requireNonNull, Optional |
ArrayIndexOutOfBoundsException (is an IndexOutOfBoundsException) | index < 0 or >= length | valid range 0 to length-1 |
ClassCastException | invalid downcast | instanceof before cast |
NumberFormatException (is an IllegalArgumentException) | Integer.parseInt("abc") | validate input |
Exceptions Cheat Sheet
1. Hierarchy
- 1Throwableroot
- 2ErrorJVM, unchecked
- 3Exceptionchecked
- 4RuntimeExceptionunchecked
2. Order and keywords
- try
- Resources close
- Matching catch
- finally
| Keyword | Does |
|---|---|
throw | raises an object |
throws | declares in signature |
finally | always cleans up |
Check yourself
What does this print?
F, then1finallyruns before the method returns, soFprints first
static int f() { try { return 1; } finally { System.out.println("F"); } } System.out.println(f());
Why does this not compile?
Exceptionalready catchesIOException, so the second catch is unreachable- Put the subclass first
try { read(); } catch (Exception e) { } catch (IOException e) { }
In try-with-resources, the body throws A and close() throws B. Which does the caller see?
- A, with B attached through
getSuppressed() - A manual
finally { close() }would throw B and lose A