Skip to main content
PreparationZone Logo
PrepZone

The Exception Hierarchy

Checked versus unchecked, Error versus Exception, and which branch you should never catch.

Pass checkpoint below

Read these first

Why this matters

  • The checked-versus-unchecked distinction decides whether the compiler forces a caller to deal with your failure, which is an API design decision rather than a technical one.
  • Catching the wrong branch of the hierarchy turns a recoverable problem into a hidden one, or a fatal problem into a corrupted program that keeps running.
  • Reading a stack trace correctly is the single highest-value debugging skill, and most people read it in the wrong direction.

The hierarchy

Throwable
ErrorOutOfMemoryError — do not catch
ExceptionChecked — compiler enforced
RuntimeExceptionUnchecked — your bugs

RuntimeException sits under Exception but escapes the checked rule — that single exception to the rule is what the whole checked/unchecked distinction rests on.

Everything you can throw descends from Throwable. Only the checked branch forces you to declare or handle it; Error and RuntimeException do not.
  • Throwable is the root. Only its subtypes may appear in a throw or a catch.
  • Error signals a condition outside your program's control — the heap is exhausted, a stack overflowed, a class is missing. Do not catch these.
  • Exception signals an application-level problem. Everything here is fair game to handle.
  • RuntimeException, a subclass of Exception, is the unchecked branch. The compiler does not require handling it.
  • Every other Exception subclass is checked, and the compiler insists you either catch it or declare it.

Checked versus unchecked

The compiler's rule is purely structural: is the type a RuntimeException or an Error? If not, it is checked.

AspectCheckedUnchecked
ExamplesIOException, SQLException, ClassNotFoundExceptionNullPointerException, IllegalArgumentException, IllegalStateException
Compiler requires handlingYes — catch or declareNo
SignalsAn expected external failureA programming error, usually
Caller can recoverOften — retry, fall back, reportRarely — the code needs fixing
Appears in the signatureMust, via throwsOptional, for documentation
  • Examples

    CheckedIOException, SQLException, ClassNotFoundException
    UncheckedNullPointerException, IllegalArgumentException, IllegalStateException
  • Compiler requires handling

    CheckedYes — catch or declare
    UncheckedNo
  • Signals

    CheckedAn expected external failure
    UncheckedA programming error, usually
  • Caller can recover

    CheckedOften — retry, fall back, report
    UncheckedRarely — the code needs fixing
  • Appears in the signature

    CheckedMust, via throws
    UncheckedOptional, for documentation

The intent is that checked means 'something outside went wrong' and unchecked means 'the code is wrong'.

Java
Loading compiler…

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Still to come · 5 sections

  • Common exceptions and what they really mean
  • Reading a stack trace
  • Wrapping preserves the cause
  • Throwing well
  • Common misreadings

Test yourself

Answer these before moving on — recall is what makes it stick.