GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Unreachable Code Error in Java - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

Unreachable Code Error in Java

Last Updated : 11 Sep, 2026

Unreachable code refers to statements that the compiler determines can never be executed during program execution. Such statements usually occur when a control-flow statement, such as return, break, continue, or throw, prevents execution from reaching the following code.

  • Unreachable statements can result in a compile-time error.
  • Common causes include statements placed after return, break, continue, or throw.

Common Cases Of Unreachable Code

Unreachable code occurs when a control-flow statement prevents the program from reaching a subsequent statement. Common cases include statements placed after return, break, continue, or throw.

1. Unreachable Code After return

The return statement terminates the execution of a method and returns control to the caller. Therefore, any statement written after an unconditional return statement cannot be executed.

Java
class ReturnDemo {

    public static void main(String[] args) {

        System.out.println("This statement will be printed");

        return;

        // This statement is unreachable
        System.out.println("This statement will not be printed");
    }
}

Output:

./ReturnDemo.java:10: error: unreachable statement
System.out.println("This statement will not be printed");
^
1 error

Explanation: The return statement ends the execution of the main() method. Therefore, the System.out.println() statement placed after return can never be executed. The Java compiler detects this and reports an unreachable statement error.

2. Unreachable Code After break

The break statement immediately terminates the execution of the loop or switch statement in which it appears. Therefore, a statement placed after an unconditional break within the same block is unreachable.

Java
class BreakDemo {

    public static void main(String[] args) {

        for (int i = 1; i <= 5; i++) {

            break;

            // This statement is unreachable
            System.out.println("Value of i: " + i);
        }
    }
}

Output:

./BreakDemo.java:10: error: unreachable statement
System.out.println("Value of i: " + i);
^
1 error

Explanation: The break statement immediately exits the for loop. As a result, the System.out.println() statement after break can never execute within that iteration. The compiler therefore reports it as an unreachable statement.

3. Unreachable Code After throw

The throw statement is used to explicitly throw an exception. Once a throw statement is executed, control leaves the current flow and the statements immediately following it are not executed.

Java
class ThrowDemo {

    public static void main(String[] args) {

        try {
            throw new Exception("Something went wrong");

            // This statement is unreachable
            System.out.println("Program continues");
        }
        catch (Exception e) {

            System.out.println(e.getMessage());
        }
    }
}

Output:

./ThrowDemo.java:9: error: unreachable statement
System.out.println("Program continues");
^
1 error

Explanation: The throw statement throws an exception before the System.out.println() statement can be reached. Control is transferred to the appropriate catch block. Therefore, the statement following throw is unreachable and causes a compile-time error.

4. Unreachable Code After continue

The continue statement skips the remaining statements in the current loop iteration and transfers control to the next iteration of the loop. Therefore, a statement immediately following an unconditional continue is unreachable.

Java
class ContinueDemo {

    public static void main(String[] args) {

        for (int i = 0; i < 5; i++) {

            continue;

            // This statement is unreachable
            System.out.println("Value: " + i);
        }
    }
}

Output:

./ContinueDemo.java:10: error: unreachable statement
System.out.println("Value: " + i);
^
1 error

Explanation: The continue statement skips the rest of the current loop iteration and moves to the next iteration. Since the System.out.println() statement is placed immediately after an unconditional continue, it can never be executed. The compiler reports it as an unreachable statement.

Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.