Part 11 / Topic 1 of 10
Exceptions, revisited: designing your own
Part 6 taught you to catch exceptions the JDK throws at you. This part is about throwing your own, which is a design decision, not just error handling — the exceptions you have been living with since Part 6 (NumberFormatException, ArrayIndexOutOfBoundsException) were somebody else's design decisions. Now that you are about to call real I/O (HTTP, a database) that fails in ways worth naming, it is time to make a few of those decisions yourself.
Think of a hospital's intake desk: a patient with an incomplete form (fixable, right there, by the patient) is a different kind of problem from the x-ray machine being broken (not something the person at the desk can fix at all) — both are "something went wrong," but only one of them should stop and wait for the person who hit it to do something about it immediately.
The practical rule from Part 6 still holds, now applied to exceptions you write:
| Use | When | Because |
|---|---|---|
checked (extends Exception) |
the immediate caller can realistically recover, one call at a time | the compiler forces every caller to decide what to do, which is worth the friction here |
unchecked (extends RuntimeException) |
the caller can't fix it inline (a database is down, a file system is full) | forcing a try/catch on every call site just to rethrow or log is noise |
A second, related decision: when your code catches a low-level exception it cannot usefully act on, wrap it in your own exception instead of letting the raw type (or its message alone) leak out of your API. Pass the original exception as the cause, so nothing is lost — getCause() (and the printed stack trace) still shows exactly what broke underneath.
class InvalidOrderLineException extends Exception { // checked: skip this ONE line and move on
public InvalidOrderLineException(String message) { super(message); }
}
class OrderStoreException extends RuntimeException { // unchecked: the whole store is unusable
public OrderStoreException(String message, Throwable cause) {
super(message, cause); // keeps the original exception as the cause
}
}
$ javac ExceptionDesignDemo.java
$ java ExceptionDesignDemo
-- parsing (checked exception, one line at a time) --
Parsed: Order[city=Pune, amount=1200.0]
Skipped: amount is not a number in: Delhi,abc
Skipped: expected 'city,amount' but got: Chennai
Parsed: Order[city=Mumbai, amount=2500.0]
-- saving (unchecked exception, with a wrapped cause) --
Fatal: could not save order for Pune
Caused by: java.lang.RuntimeException: connection refused: localhost:5433
parseOrderLine throws the checked InvalidOrderLineException so every bad line can be skipped individually, and the good ones still get processed. saveOrder throws the unchecked OrderStoreException, wrapping whatever actually failed underneath (here, a simulated RuntimeException standing in for a real connection failure) — the caller is told clearly what failed and why, without being forced to add a try/catch around every single save call in the program.
🔍 Going deeper: a custom exception does not need to add any fields or methods at all — the class name alone (
OrderStoreExceptionvs. plainRuntimeException) is documentation, and lets acatchblock target exactly your failure without also catching every unrelatedRuntimeExceptionin the program.
Full program: examples/ExceptionDesignDemo.java.
Done here? The practice set for Part 11 — including the solutions — is on the part page.