Exceptions
On this page 8 topics
Overview
Topics
- Checked vs unchecked
- try/catch/finally
- throw/throws
- custom exceptions
- try-with-resources
Learn first
95 min of reading · 4 h of practice (estimate)
Learning outcomes
- Distinguish checked exceptions, unchecked exceptions, and errors, and explain the hierarchy.
- Handle exceptions with
try/catch/finallyand multi-catch, catching the narrowest useful type. - Throw exceptions with meaningful messages and declare them with
throws. - Create a custom exception for a business rule violation.
- Release resources with try-with-resources.
Lessons
- 01What goes wrong at runtime12 min read
- 02The exception hierarchy12 min read
- 03try, catch, finally12 min read
- 04Throwing and declaring12 min read
- 05Custom exceptions12 min read
- 06try-with-resources12 min read
- 07Demo: InsufficientFundsException end to end11 min read
- 08Demo: finding a swallowed exception12 min read
Lab and exercises
Lab
BEG-029-LAB1 · Bank with rules
Outcomes: 1–5 · Time: ~2 h Build an Account with deposit and withdraw. Overdrawing throws InsufficientFundsException. A console loop reads amounts as text and retries when NumberFormatException occurs. Read one small file using try-with-resources. Acceptance: no empty catch blocks; messages helpful to the user.
Exercises (assessment: five exercises plus a "fix the swallowed exception" review)
| ID | Outcome | Task |
|---|---|---|
| BEG-029-EX01 | 1 | Classify as checked, unchecked, or error: IOException, NullPointerException, OutOfMemoryError, IllegalArgumentException. |
| BEG-029-EX02 | 2 | Write code that parses a string to int and prints a friendly message on failure, plus a finally that prints "done". |
| BEG-029-EX03 | 2 | Rewrite two identical catch blocks for NumberFormatException and ArithmeticException as one multi-catch. |
| BEG-029-EX04 | 3, 4 | Write OutOfStockException (with a cause constructor) and a method that throws it with a message naming the item and quantity. |
| BEG-029-EX05 | 5 | Rewrite a BufferedReader read with manual finally { close(); } as try-with-resources. |
| BEG-029-EX06 | 2 | Review: fix try { load(); } catch (Exception e) {} so the error is not swallowed. |
Show solutions
- EX01 Checked, unchecked, error, unchecked.
- EX02
try { int n = Integer.parseInt(s); } catch (NumberFormatException e) { System.out.println("'" + s + "' is not a whole number."); } finally { System.out.println("done"); } - EX03
catch (NumberFormatException | ArithmeticException e) { ... }. - EX04
class OutOfStockException extends Exception { OutOfStockException(String m){super(m);} OutOfStockException(String m, Throwable c){super(m,c);} }thenthrow new OutOfStockException("Only 2 of 'pen' left, requested 5"); - EX05
try (BufferedReader r = Files.newBufferedReader(p)) { ... } catch (IOException e) { ... }. - EX06 Catch the narrow type (for example
IOException), print a clear message, and either recover or rethrownew IllegalStateException("Could not load data", e). - Outcome 1 is also covered by quiz Q01–Q03; outcome 3 is also covered by the lab.
Quiz
Outcomes: O1 hierarchy · O2 try/catch/finally · O3 throw and throws · O4 custom exceptions · O5 try-with-resources.
A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.
Practical part
Five exercises BEG-029-EX01 to EX05 plus the swallowed-exception review EX06 and the lab. Pass: at least four exercises correct, the review fixed, and no empty catch in the lab.
Common mistakes
catch (Exception e) {}; using exceptions for normal control flow; losing the original cause.
Assessment
5 exercises plus a "fix the swallowed exception" review.
Career relevance
API error handling (JNR-015) and transaction rollback rules (JNR-031).