Skip to content
Ameen Abozaid
BEG-029

Exceptions

  • 8 Lessons
  • 95 min of reading
  • 4 h practice (estimate)
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

  1. Distinguish checked exceptions, unchecked exceptions, and errors, and explain the hierarchy.
  2. Handle exceptions with try/catch/finally and multi-catch, catching the narrowest useful type.
  3. Throw exceptions with meaningful messages and declare them with throws.
  4. Create a custom exception for a business rule violation.
  5. Release resources with try-with-resources.

Lessons

  1. 01What goes wrong at runtime12 min read
  2. 02The exception hierarchy12 min read
  3. 03try, catch, finally12 min read
  4. 04Throwing and declaring12 min read
  5. 05Custom exceptions12 min read
  6. 06try-with-resources12 min read
  7. 07Demo: InsufficientFundsException end to end11 min read
  8. 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)

IDOutcomeTask
BEG-029-EX011Classify as checked, unchecked, or error: IOException, NullPointerException, OutOfMemoryError, IllegalArgumentException.
BEG-029-EX022Write code that parses a string to int and prints a friendly message on failure, plus a finally that prints "done".
BEG-029-EX032Rewrite two identical catch blocks for NumberFormatException and ArithmeticException as one multi-catch.
BEG-029-EX043, 4Write OutOfStockException (with a cause constructor) and a method that throws it with a message naming the item and quantity.
BEG-029-EX055Rewrite a BufferedReader read with manual finally { close(); } as try-with-resources.
BEG-029-EX062Review: 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);} } then throw 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 rethrow new 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.

  1. 1Which is a checked exception?O1
  2. 2NullPointerException is:O1
  3. 3Which should normal code not try to catch?O1
  4. 4When does finally run?O2
  5. 5With two catch blocks, the more specific type should come:O2
  6. 6The best catch is:O2
  7. 7throws IOException on a method means:O3
  8. 8A good exception message:O3
  9. 9A custom checked exception extends:O4
  10. 10try-with-resources requires the resource to implement:O5

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).

Type to search levels, phases, modules and lessons.