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.
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.
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.
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.
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.