Javatpoint Logo
Javatpoint Logo

Unreachable Code Error in Java

One of the common errors that a developer or programmer faces is the unreachable code error in Java. The unreachable code error occurs when it is not possible to execute one or more than one statement in Java. For example, if we write a statement after the return statement, then that statement will not execute and hence, results in an unreachable code error. In this tutorial, we are going to cover various scenarios where we get unreachable code errors in Java.

Scenarios for Unreachable Code Error

Observe the scenarios where we get the unreachable code error.

1) Infinite Loop

Image what will happen if we write statement after a loop that will run infinite times. The control will be trapped in the infinite loop forever, and it will never reach the statements that are written after the loop. The following program shows the same.

FileName: InfiniteLoop.java

Output:

/InfiniteLoop.java:12: error: unreachable statement
System.out.println("Print statement written after the infinite loop.");
^
1 error

Explanation: The control will never reach the print statement, and hence we get the error as shown in the output.

2) Statements After the return Statement

When a return statement gets executed, the control comes out of the method in which the return statement is written. So, there is no chance of the execution of statements that are written after the return statement. The illustration of the same is mentioned in the following program.

FileName: AfterReturnSttment.java

Output:

/AfterReturnSttment.java:8: error: unreachable statement
System.out.println("Print statement written after the return statement");
^
1 error

Explanation: After the return statement, there is no way for the JVM to access the print statement throughout the life cycle of this program. Hence, we get the unreachable error as shown in the output.

3) Statements After Throwing an Exception

In a try-catch block, if we write some statements immediately after throwing an exception, then those statements become unreachable, and we get the unreachable statement error. See the following program for a better understanding.

FileName: AfterThrowSttment.java

Output:

/AfterThrowSttment.java:13: error: unreachable statement
System.out.println("statement after the exception is thrown.");
^
1 error

Explanation: When the throw new exception statement is executed, the control jumps and reaches either to finally or the catch block. Hence, there is no chance of the execution of the statements that are written after the throw statement resulting in the unreachable statement error.

4) Statements After the break Statement

Sometimes writing statements after the break statements also leads to unreachable errors. Observe the following example.

FileName: AfterBreakSttment.java

Output:

/AfterBreakSttment.java:14: error: unreachable statement
System.out.println("statement after the break statement.");
^
1 error

Explanation: By analyzing the code, it becomes evident that the break statement will come into action, and hence, the control will come out of the loop, making the print statement inaccessible. Therefore, we get the unreachable error in the output.

5) Statements After the continue Statement

Sometimes writing statements after the continue statement also leads to unreachable error. Observe the following example.

FileName: AfterContinueSttment.java

Output:

/AfterContinueSttment.java:13: error: unreachable statement
System.out.println("statement after the continue statement.");
^
1 error

Explanation: In the program, the if condition is written in such a way that the continue statement gets executed in each iteration of the for-loop. Therefore, it is not possible for the JVM to get access to the print statement that is written immediately after the continue statement resulting in the error as shown in the output.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA