Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Java
Stage 5: Specialization

Part 06 / Topic 5 of 8

Checked vs. unchecked exceptions

Every exception in Java is a subtype of Throwable, but for this lesson only one split matters:

Examples you've seen Compiler forces you to handle it? Usually means
Unchecked (RuntimeException and its subtypes) NumberFormatException, ArrayIndexOutOfBoundsException, ArithmeticException No A bug, or input you forgot to validate
Checked (Exception, but not RuntimeException) InterruptedException, java.io.IOException (Part 7) Yes Something that can fail even in correct code (a wait gets interrupted, a file isn't there)

"Checked" means the compiler checks, at compile time, that you did one of two things with it:

  • catch it, or
  • declare it with throws ExceptionType on your method, pushing the decision to whoever calls your method.

Skip both, and javac refuses to compile. Thread.sleep(milliseconds) is a good checked example you already have everything needed to use: it pauses the current thread, but another thread could interrupt the wait, so it throws the checked InterruptedException.

static void pauseForOneSecond() {
    try {
        Thread.sleep(1000);
        System.out.println("  Done pausing.");
    } catch (InterruptedException e) {
        System.out.println("  Pause was interrupted: " + e.getMessage());
    }
}

Real output (examples/04_checked_vs_unchecked.java):

$ javac 04_checked_vs_unchecked.java
$ java CheckedVsUnchecked
== Checked exception (InterruptedException), handled with try/catch ==
  Pausing for processing (1 second)...
  Done pausing.

== Unchecked exception (ArithmeticException), no catch required to compile ==
10% of 1500 = 150

percentOf divides and could throw ArithmeticException if percent made the division blow up, but since it's unchecked, javac does not force a catch — the method compiles with no handling at all.

What happens if you skip handling a checked exception? examples/CheckedWithoutHandling.java calls Thread.sleep with no catch and no throws, on purpose:

static void pauseBroken() {
    System.out.println("Pausing...");
    Thread.sleep(1000);   // compile error: InterruptedException is checked, unhandled here
    System.out.println("Done.");
}

Real javac output:

$ javac CheckedWithoutHandling.java
CheckedWithoutHandling.java:25: error: unreported exception InterruptedException; must be caught or declared to be thrown
        Thread.sleep(1000);   // compile error: InterruptedException is checked, unhandled here
                    ^
1 error

This file is not meant to run — it exists to show the real error message. Fix it either way shown above: wrap it in try/catch, or add throws InterruptedException to the method signature.

⚠️ This is only the practical first pass. Designing your own exception hierarchy, deciding when a custom exception should be checked instead of unchecked, and handling exceptions at scale with real I/O comes back in Stage 2 Part 11, once file and network I/O give it something real to practice on.

Full programs: examples/04_checked_vs_unchecked.java, examples/CheckedWithoutHandling.java (does not compile, on purpose).

Done here? The practice set for Part 6 — including the solutions — is on the part page.

Search the guide