Core Java

core18ExceptionsDemo

Path
pkg1core/core18ExceptionsDemo.java
Package
pkg1core
Study order
19
Run
Single-file source launch
Command
java pkg1core/core18ExceptionsDemo.java
Lesson
Back to the chapter

There is no in-browser runner. This is the file from the curriculum, unchanged.

pkg1core/core18ExceptionsDemo.java
1package pkg1core;2 3/*4 * core18ExceptionsDemo.java5 * -------------------6 * Checked vs unchecked, try/catch/finally, multi-catch, try-with-resources,7 * custom exceptions, and exception chaining.8 *9 * EXPLANATION:10 *  - Throwable -> Error (don't catch) and Exception.11 *  - Checked exceptions (extend Exception) must be declared/handled.12 *  - Unchecked (extend RuntimeException) are programming errors.13 *  - try-with-resources auto-closes AutoCloseable resources.14 */15public class core18ExceptionsDemo {16 17    static class InsufficientFundsException extends Exception {  // checked, custom18        InsufficientFundsException(String msg) { super(msg); }19    }20 21    // A resource that auto-closes22    static class Resource implements AutoCloseable {23        Resource() { System.out.println("  open resource"); }24        void use() { System.out.println("  use resource"); }25        public void close() { System.out.println("  close resource (auto)"); }26    }27 28    static void withdraw(double balance, double amt) throws InsufficientFundsException {29        if (amt > balance) throw new InsufficientFundsException("need " + amt + " have " + balance);30        System.out.println("withdrew " + amt);31    }32 33    public static void main(String[] args) {34        // try / catch / finally35        try {36            int[] a = new int[2];37            System.out.println(a[5]);                   // throws ArrayIndexOutOfBounds38        } catch (ArrayIndexOutOfBoundsException e) {39            System.out.println("Caught: " + e.getClass().getSimpleName());40        } finally {41            System.out.println("finally always runs");42        }43 44        // Multi-catch45        try {46            Object o = "x";47            Integer i = (Integer) o;                    // ClassCastException48        } catch (NullPointerException | ClassCastException e) {49            System.out.println("Multi-catch handled: " + e.getClass().getSimpleName());50        }51 52        // Checked custom exception53        try {54            withdraw(100, 150);55        } catch (InsufficientFundsException e) {56            System.out.println("Business error: " + e.getMessage());57        }58 59        // try-with-resources60        try (Resource r = new Resource()) {61            r.use();62        }63 64        // Exception chaining (preserve the cause)65        try {66            try { throw new java.io.IOException("disk error"); }67            catch (java.io.IOException io) { throw new RuntimeException("wrapping", io); }68        } catch (RuntimeException e) {69            System.out.println("Wrapped: " + e.getMessage() + " caused by " + e.getCause().getMessage());70        }71    }72}