Home › Java

error: unreachable statement (Java) — how to fix

Main.java:7: error: unreachable statement
        System.out.println("done");
        ^
1 error

Quick fix: Move or delete the code after the return/throw/break/continue. If you temporarily wanted to skip code, wrap it in if (true) return; or comment it out. For code after while (true) without a break, add a break condition or remove the trailing code.

What it means

The Java Language Specification (JLS 14.22) requires every statement to be reachable by a simple flow analysis. If a statement can't be reached, javac stops with unreachable statement. The caret points at the first dead statement; the cause is usually just above it.

Cause 1: code after return or throw

int total(int[] xs) {
    int sum = 0;
    for (int x : xs) sum += x;
    return sum;
    System.out.println("sum computed");   // error
}

Move it before the return:

    System.out.println("sum computed");
    return sum;

Same for throw:

throw new IllegalStateException("bad state");
cleanup();   // error: never runs

Use finally if the code must run in both cases.

Cause 2: code after break or continue in a loop/switch

for (String s : items) {
    if (s.isEmpty()) {
        continue;
        count++;          // error
    }
}

switch (cmd) {
    case "stop":
        running = false;
        break;
        log("stopped");   // error
}

Put the statement before break/continue.

Cause 3: code after an infinite loop

while (true) {
    handle(queue.take());
}
System.out.println("exit");   // error: loop never ends normally

The compiler knows while (true) (or for (;;)) without a break never completes. Add an exit:

while (running) {
    handle(queue.take());
}
System.out.println("exit");

or break out of it on a condition. Note while (flag) with a non-constant variable is fine; only constant true triggers the analysis.

Cause 4: temporarily disabling code with return

A quick return; at the top of a method to skip the rest fails to compile. Use a condition the compiler doesn't treat as constant-folded dead code:

if (true) return;     // allowed: if statements are exempt from this check

The JLS deliberately exempts if so you can do conditional compilation like if (DEBUG) { ... }. Commenting out the code is cleaner.

Cause 5: catch block for an exception that can't be thrown

A related compile error: exception IOException is never thrown in body of corresponding try statement. Also, catching a subclass after its superclass gives exception FileNotFoundException has already been caught:

try { ... }
catch (IOException e) { ... }
catch (FileNotFoundException e) { ... }   // error: already caught

Order catch blocks from most specific to most general.

The opposite analysis: a method's end is reachable but it must return a value. See missing return statement.

Kotlin and IDEs

Kotlin reports Unreachable code only as a warning. IntelliJ highlights dead code in grey before you compile, which is the quickest way to spot it.

Last updated 2026-11-23.