Checked vs Unchecked Exceptions in Java

Checked exceptions must be declared or caught at compile time; unchecked exceptions (RuntimeException) don't require handling. Learn when to use each.

Published September 22, 2026

Checked exceptions must be caught or declared with throws, and the compiler enforces it; unchecked exceptions (RuntimeException and subclasses) need no declaration. Use checked for recoverable conditions and unchecked for programming errors.

The concept

Checked exceptions extend Exception but not RuntimeException (for example IOException). They represent situations a caller can reasonably handle, like a missing file.

Unchecked exceptions (NullPointerException, IllegalArgumentException) usually indicate bugs. Errors such as OutOfMemoryError are also unchecked and should not be caught.

Hierarchy

Throwable
├─ Error (unchecked)
└─ Exception (checked)
└─ RuntimeException (unchecked)

Practical examples

Checked: must handle

try (var in = new java.io.FileInputStream("a.txt")) {
  in.read();
} catch (java.io.IOException e) {
  System.err.println("cannot read: " + e.getMessage());
}

Unchecked: validate input

void setAge(int age) {
  if (age < 0) throw new IllegalArgumentException("age < 0");
}

Custom exceptions

class OrderNotFound extends RuntimeException {
  OrderNotFound(long id) { super("Order " + id + " not found"); }
}

Wrap to keep the cause

catch (IOException e) { throw new UncheckedIOException(e); }

Common mistakes

  • Swallowing exceptions with an empty catch.
  • Catching Exception or Throwable too broadly.
  • Declaring throws Exception everywhere.
  • Dropping the original cause when rethrowing.

Key takeaways

  • Checked = compiler-enforced, recoverable.
  • Unchecked = bugs or fatal conditions.
  • Always preserve the cause.

FAQ

Which does modern Java prefer?

Many frameworks favour unchecked exceptions to reduce clutter.

What is try-with-resources?

A try that automatically closes AutoCloseable resources.

More Java articles