Home › Learn › Java › Exceptions, and the project

Custom and checked exceptions

Lesson 37 of 40. Define your own exception type and make callers deal with it.

Your own exception is a class that extends an existing one: class LibraryException extends Exception { LibraryException(String message) { super(message); } }. Callers can then catch exactly your kind. Extending Exception makes it checked: a method that may throw it must say so with throws LibraryException in its signature, and every caller must either catch it or declare it too. Extending RuntimeException makes it unchecked, with no declarations required; Java's own IllegalArgumentException is one of those.

Checked exceptions suit conditions the caller is expected to handle as part of normal operation, such as a book that is not available; unchecked ones suit programming mistakes. The library project throws LibraryException, the class from this lesson, for everything a librarian would explain to a member.

class ParseException extends Exception {
    ParseException(String message) {
        super(message);
    }
}

static int parseAge(String text) throws ParseException {
    if (text.isEmpty()) {
        throw new ParseException("no age given");
    }
    return Integer.parseInt(text);
}

Your task

Write class LibraryException extends Exception with a constructor taking the message. Then, in Main, write static int safeDivide(int a, int b) throws LibraryException, which throws a LibraryException with the message cannot divide by zero when b is 0 (check before dividing) and otherwise returns a / b.

Finally, static String attempt(int a, int b) returning <a> / <b> = <result> or, when safeDivide throws, error: <message>.

Part of the final project. LibraryException is what the library throws when a request cannot be honored: unknown book, book out, bad card. It is saved in this browser and loaded into Project: the library when you get there.

Hint 1

class LibraryException extends Exception { LibraryException(String message) { super(message); } }

Hint 2

safeDivide: if (b == 0) throw new LibraryException("cannot divide by zero"); attempt: try { return a + " / " + b + " = " + safeDivide(a, b); } catch (LibraryException e) { return "error: " + e.getMessage(); }

Show the solution
public class Main {
    static int safeDivide(int a, int b) throws LibraryException {
        if (b == 0) {
            throw new LibraryException("cannot divide by zero");
        }
        return a / b;
    }

    static String attempt(int a, int b) {
        try {
            return a + " / " + b + " = " + safeDivide(a, b);
        } catch (LibraryException e) {
            return "error: " + e.getMessage();
        }
    }

    public static void main(String[] args) {
        System.out.println(attempt(10, 2) + " / " + attempt(1, 0));
    }
}

class LibraryException extends Exception {
    LibraryException(String message) {
        super(message);
    }
}

    Compiles with OpenJDK javac and TeaVM, both compiled to WebAssembly (7 MB download the first time, then cached), and runs in this tab; nothing is sent anywhere.