Explain the Java exception hierarchy and the difference between checked and unchecked exceptions. When would you create each kind?
Show the explanation
Everything thrown extends Throwable, which has two branches:
Error— serious JVM problems (OutOfMemoryError,StackOverflowError). Applications normally do not catch these.Exception— conditions a program may handle.- Checked exceptions:
Exceptionand its subclasses exceptRuntimeException. The compiler forces you to catch them or declare them withthrows(e.g.IOException,SQLException). - Unchecked exceptions:
RuntimeExceptionand its subclasses (NullPointerException,IllegalArgumentException,ArrayIndexOutOfBoundsException). No compile-time requirement.
class SeatUnavailableException extends Exception { // checked: callers must plan for it
SeatUnavailableException(String seat) { super("Seat " + seat + " is taken"); }
}
void book(String seat) throws SeatUnavailableException {
if (seat == null) throw new IllegalArgumentException("seat is required"); // unchecked: caller bug
if (taken.contains(seat)) throw new SeatUnavailableException(seat);
taken.add(seat);
}Rule of thumb: use a checked exception for an expected, recoverable situation the caller should handle; use an unchecked one for programming errors (bad arguments, broken invariants). Many modern libraries lean towards unchecked exceptions to avoid long throws lists.
Original UniCircle question. Last reviewed 22 Oct 2026.
