I only just noticed this question, and wanted to add my $.02 to this.
In case of Java, this is not actually an option. The «unreachable code» error doesn’t come from the fact that JVM developers thought to protect developers from anything, or be extra vigilant, but from the requirements of the JVM specification.
Both Java compiler, and JVM, use what is called «stack maps» — a definite information about all of the items on the stack, as allocated for the current method. The type of each and every slot of the stack must be known, so that a JVM instruction doesn’t mistreat item of one type for another type. This is mostly important for preventing having a numeric value ever being used as a pointer. It’s possible, using Java assembly, to try to push/store a number, but then pop/load an object reference. However, JVM will reject this code during class validation,- that is when stack maps are being created and tested for consistency.
To verify the stack maps, the VM has to walk through all the code paths that exist in a method, and make sure that no matter which code path will ever be executed, the stack data for every instruction agrees with what any previous code has pushed/stored in the stack. So, in simple case of:
Object a;
if (something) { a = new Object(); } else { a = new String(); }
System.out.println(a);
at line 3, JVM will check that both branches of ‘if’ have only stored into a (which is just local var#0) something that is compatible with Object (since that’s how code from line 3 and on will treat local var#0).
When compiler gets to an unreachable code, it doesn’t quite know what state the stack might be at that point, so it can’t verify its state. It can’t quite compile the code anymore at that point, as it can’t keep track of local variables either, so instead of leaving this ambiguity in the class file, it produces a fatal error.
Of course a simple condition like if (1<2) will fool it, but it’s not really fooling — it’s giving it a potential branch that can lead to the code, and at least both the compiler and the VM can determine, how the stack items can be used from there on.
P.S. I don’t know what .NET does in this case, but I believe it will fail compilation as well. This normally will not be a problem for any machine code compilers (C, C++, Obj-C, etc.)
Improve Article
Save Article
Improve Article
Save Article
The Unreachable statements refers to statements that won’t get executed during the execution of the program are called Unreachable Statements. These statements might be unreachable because of the following reasons:
- Have a return statement before them
- Have an infinite loop before them
- Any statements after throwing exception in a try block
Scenarios where this error can occur:
- Have a return statement before them: When a return statement gets executed, then that function execution gets stopped right there. Therefore any statement after that wont get executed. This results in unreachable code error.
Example:
Java
class GFG {
public static void main(String args[])
{
System.out.println("I will get printed");
return;
System.out.println("I want to get printed");
}
}
- Compile Errors:
prog.java:11: error: unreachable statement
System.out.println(“I want to get printed”);
^
1 error
- Have an infinite loop before them: Suppose inside “if” statement if you write statements after break statement, then the statements which are written below “break” keyword will never execute because if the condition is false, then the loop will never execute. And if the condition is true, then due to “break” it will never execute, since “break” takes the flow of execution outside the “if” statement.
Example:
Java
class GFG {
public static void main(String args[])
{
int a = 2;
for (;;) {
if (a == 2)
{
break;
System.out.println("I want to get printed");
}
}
}
}
- Compile Errors:
prog.java:13: error: unreachable statement
System.out.println(“I want to get printed”);
^
1 error
- Any statement after throwing an exception: If we add any statements in a try-catch block after throwing an exception, those statements are unreachable because there is an exceptional event and execution jumps to catch block or finally block. The lines immediately after the throw is not executed.
Example:
Java
class GFG {
public static void main(String args[])
{
try {
throw new Exception("Custom Exception");
System.out.println("Hello");
}
catch (Exception exception) {
exception.printStackTrace();
}
}
}
- Compile Errors:
prog.java:7: error: unreachable statement
System.out.println(“Hello”);
^
1 error
- Any statement after writing continue : If we add any statements in a loop after writing the continue keyword, those statements are unreachable because execution jumps to the top of the for loop. The lines immediately after the continue is not executed.
Example:
Java
class GFG {
public static void main(String args[])
{
for (int i = 0; i < 5; i++)
{
continue;
System.out.println("Hello");
}
}
}
- Compile Errors:
prog.java:6: error: unreachable statement
System.out.println(“Hello”);
^
1 error
Does it mean that compiler checks whether it is able to compile for all branches/lines of code ?
It means the compiler checks that every statement is reachable.
From section 14.21 of the JLS:
It is a compile-time error if a statement cannot be executed because it is unreachable.
This section is devoted to a precise explanation of the word «reachable.» The idea is that there must be some possible execution path from the beginning of the constructor, method, instance initializer, or static initializer that contains the statement to the statement itself. The analysis takes into account the structure of statements.
The section then documents how reachability is defined.
In particular, the relevant points in your case are:
Every other statement S in a non-empty block that is not a switch block is reachable iff the statement preceding S can complete normally.
A
break,continue,return, orthrowstatement cannot complete normally.
So your «line 1» statement is preceded by a statement (break;) which cannot complete normally, and therefore it’s unreachable.
Does it mean that compiler checks whether it is able to compile for all branches/lines of code ?
It means the compiler checks that every statement is reachable.
From section 14.21 of the JLS:
It is a compile-time error if a statement cannot be executed because it is unreachable.
This section is devoted to a precise explanation of the word «reachable.» The idea is that there must be some possible execution path from the beginning of the constructor, method, instance initializer, or static initializer that contains the statement to the statement itself. The analysis takes into account the structure of statements.
The section then documents how reachability is defined.
In particular, the relevant points in your case are:
Every other statement S in a non-empty block that is not a switch block is reachable iff the statement preceding S can complete normally.
A
break,continue,return, orthrowstatement cannot complete normally.
So your «line 1» statement is preceded by a statement (break;) which cannot complete normally, and therefore it’s unreachable.
In this post, we will look into Unreachable Statement Java Error, when does this occurs, and its resolution.
1. Introduction

An unreachable Statement is an error raised as part of compilation when the Java compiler detects code that is never executed as part of the execution of the program. Such code is obsolete, unnecessary and therefore it should be avoided by the developer. This code is also referred to as Dead Code, which means this code is of no use.
2. Explanation
Now that we have seen what actually is Unreachable Statement Error is about, let us discuss this error in detail about its occurrence in code with few examples.
Unreachable Statement Error occurs in the following cases
- Infinite Loop before a line of code or statements
- Statements after return, break or continue
2.1 Infinite Loop before a line of code or statements
In this scenario, a line of code is placed after an infinite loop. As the statement is placed right after the loop, this statement is never executed as the statements in the loop get executed repeatedly and the flow of execution is never terminated from the loop. Consider the following code,
Example 1
public class JavaCodeGeeks
{
public static void main(String[] args) {
while(true)
{
System.out.println("Hi");
}
System.out.println("Hello");
}
}
Output
JavaCodeGeeks.java:8: error: unreachable statement
System.out.println("Hello");
In this example, compiler raises unreachable statement error at line 8 when we compile the program using javac command. As there is an infinite while loop before the print statement of “hello”, this statement never gets executed at runtime. If a statement is never executed, the compiler detects it and raises an error so as to avoid such statements.
Now, let us see one more example which involves final variables in the code.Example 2
public class JavaCodeGeeks
{
public static void main(String[] args) {
final int a=10;
final int b=20;
while(a>b)
{
System.out.println("Inside while block");
}
System.out.println("Outside while block");
}
}
Output
JavaCodeGeeks.java:7: error: unreachable statement
{
^
1 error
In the above example, variables a and b are final. Final variables and its values are recognized by the compiler during compilation phase. The compiler is able to evaluate the condition a>b to false. Hence, the body of the while loop is never executed and only the statement after the while loop gets executed. While we compile the code, the error is thrown at line 7 indicating the body of the loop is unreachable.
Note: if a and b are non-final, then the compiler will not raise any error as non-final variables are recognized only at runtime.
2.2 Statements after return, break or continue
In Java language, keywords like return, break, continue are called Transfer Statements. They alter the flow of execution of a program and if any statement is placed right after it, there is a chance of code being unreachable. Let us see this with an example, with the return statement.Example 3
public class JavaCodeGeeks
{
public static void main(String[] args) {
System.out.println(foo(40));
}
public static int foo(int a)
{
return 10;
System.out.println("Inside foo()");
}
}
Output
JavaCodeGeeks.java:10: error: unreachable statement
System.out.println("Inside foo()");
^
JavaCodeGeeks.java:11: error: missing return statement
}
^
2 errors
In this example, when foo() method is called, it returns value 10 to the main method. The flow of execution inside foo() method is ended after returning the value. When we compile this code, error is thrown at line 10 as print statement written after return becomes unreachable.
Let us look into the second example with a break statement.Example 4
public class JavaCodeGeeks
{
public static void main(String[] args) {
for(int i=1;i<5;i++)
{
System.out.println(i);
break;
System.out.println("After break");
}
}
}
Output
JavaCodeGeeks.java:8: error: unreachable statement
System.out.println("After break");
^
1 error
When the above code is compiled, the compiler throws an error at line 8, as the flow of execution comes out of for loop after break statement, and the print statement which is placed after break never get executed.
Finally, let us get into our final example with a continued statement.Example 5
public class JavaCodeGeeks
{
public static void main(String[] args) {
for(int i=0;i<8;i++)
{
System.out.println(i);
if(i==5)
{
continue;
System.out.println("After continue");
}
}
}
}
Output
JavaCodeGeeks.java:10: error: unreachable statement
System.out.println("After continue");
When the above code is compiled, the compiler throws an error at line 10. At runtime as part of program execution, when the value increments to 5 if block is executed. Once the flow of execution encounters continue statement in if block, immediately the flow of execution reaches the start of for loop thereby making the print statement after continue to be unreachable.
3. Resolution
Keep these points in your mind so that you avoid this error while compilation of your code.
- Before compiling, examine the flow of execution of your code to ensure that every statement in your code is reachable.
- Avoid using statements which are not at all required or related to your programming logic or algorithm implementation.
- Avoid statements immediately after return, break , continue.
- Do not place code after infinite loops.
4. Unreachable Statement Java Error – Summary
In this post, we have learned about the Unreachable Statement Error in Java. We checked the reasons for this error through some examples and we have understood how to resolve it. You can learn more about it through Oracle’s page.
5. More articles
- java.lang.StackOverflowError – How to solve StackOverflowError
- java.lang.ClassNotFoundException – How to solve Class Not Found Exception (with video)
- java.lang.NullPointerException Example – How to handle Java Null Pointer Exception (with video)
- Try Catch Java Example
- For Each Loop Java 8 Example
- Simple while loop Java Example (with video)
- For loop Java Example (with video)
- Online Java Compiler – What options are there
- What is null in Java
6. Download the Source Code
This was an example of the error Unreachable Statement in Java.
Last updated on Jul. 23rd, 2021
The java unreachable statement is a compilation error thrown when the compiler detects a code that was not executed as part of the program. When you reach this state, it means that your program would not be executed anymore, and hence this piece of code is unnecessary and should be removed.
There are a couple of reasons that might result to this issue. Let’s discuss some of the possible causes and how to solve this:
Return Statement
Function execution ends when a return statement is called. Any statement within the function that comes after the return statement will not be executed. Thus, writing your code after this return statement results in an unreachable java statement.
Example:
public class Main
{
public static void main(String[] args) {
System.out.println("The java unreachable statement");
return;
System.out.println("I will not be printed");
}
}
Output:
Main.java:16: error: unreachable statement
System.out.println("I will not be printed");
^
1 error
In the above example, we have a print statement after the return statement. Using the return statement, we tell the control to go back to the caller explicitly; hence, any code after this will be considered a dead code as it will never be executed.
To solve this error, double-check the flow of your code and make sure the return statement is always the last line of code in a function.
Example:
public class Main
{
public static void main(String[] args) {
System.out.println("The java unreachable statement");
System.out.println("I will not be printed");
return;
}
}
Output:
The java unreachable statement
I will not be printed
Infinite loop
An infinite loop is an endless loop. Its code keeps reiterating the loop location hence any code written after the loop will never be executed.
Example:
public class Main
{
public static void main(String[] args) {
for(;;){
break;
System.out.print("I'm outside the infinite loop");
}
}
}
Output:
Main.java:10: error: unreachable statement
System.out.print("I'm outside the infinite loop");
^
1 error
The infinite loop code executes the loop forever. This may consume the CPU, preventing other programs from being executed. As programmers, we block or sleep the loop by adding the break statement to let other applications run. Any code after the break statement will never be executed. To solve this, make sure no code is written after sleeping or blocking the infinite loop.
Any statement after continue
The continue statement enforces the execution of a loop in a code. Any statement after the continue statement will not be executed because the execution jumps to the top of the loop to continue with the execution of the previous code.
Example:
public class Main
{
public static void main(String[] args) {
for (int j = 0; j < 5; j++)
{
System.out.println(j);
continue;
System.out.println("Java unreachable statement");
}
}
}
Removing the print system outside the function will make the obsolete code execute.
public class Main
{
public static void main(String[] args) {
for (int j = 0; j < 5; j++)
{
System.out.println(j);
continue;
}
System.out.println("Java unreachable statement");
}
}
Output:
0
1
2
3
4
Java unreachable statement
Any statement after break
Break statements are used in terminating a loop. Any code after the break statement means the code will not be compiled since the program’s running was terminated by the break. This will result in the java unreachable statement.
public class Main
{
public static void main(String[] args) {
for (int i = 0; i < 5; i++)
{
System.out.println(i);
break;
System.out.println("Java unreachable statement");
}
}
}
Output:
Main.java:10: error: unreachable statement
System.out.println("Java unreachable statement");
^
1 error
Any statement after throwing an exception
Any statements added in a try-catch block after throwing an exception is a dead code. This is because the exceptional event forces the execution to jump to the catch block statement.
Example:
public class Main
{
public static void main(String[] args) {
try {
throw new Exception("New Exception");
//java Unreachable code
System.out.println("Dead code");
}
catch (Exception exception) {
System.out.print("Home");
}
}
}
Output:
Main.java:9: error: unreachable statement
System.out.println("Dead code");
^
1 error
Removing the statement after the try-catch block prints ‘home.’
Home
Unreachable while loop
A while loop is a repeated block of code until a specific condition is met or true. If the condition in a while loop is never met or true, the code inside the loop will never run, resulting in an unreachable statement.
public class Main
{
public static void main(String[] args) {
while(2>5){
System.out.println("The greates value is 5");
}
}
}
Output:
Main.java:6: error: unreachable statement
while(2>5){
^
1 error
In the above code, 2 will ever be greater than 5; hence this condition will never be true. Any statement after this condition will never be executed.
How to solve java unreachable statement
Solving this error may entirely depend on your programming skills. However, there are key things that you should always not do as a programmer.
- Do not place any code after the infinite loop
- You should not put any statements after the return, break and continue statements.
- Avoid puttig any code after the tyr-catch block
Conclusion
It’s always good as a programmer to examine the flow of your code to make sure that every statement in the code is reachable. You can always draw a flowchart to get a deep understanding of the flow of loops. In this tutorial, we have discussed the possible causes of the java unreachable statement and how to solve it.
Happy learning!!
- Cause of the
unreachable statementError in Java - Solve the
unreachable statementError in Java

This tutorial demonstrates the unreachable statement error in Java.
Cause of the unreachable statement Error in Java
The unreachable statement error occurs when we try to put a statement after branching control flow statements. The branching statements include break, continue, and return which are used to jump to a different part of code.
These statements are usually included in the loop to break it, skip an iteration, or return a value. When we put a code statement immediately after these branching statements, it will throw the compilation error unreachable statement.
Here are examples of the error unreachable statement using the break, continue, and return statements:
Using break:
package delftstack;
public class Unreachable_Statement {
public static void main(String... args) {
int[] DemoArray = {350, 780, 300, 500, 120, 1024, 1350};
int DemoNumber = 1024;
for (int INTEGER : DemoArray) {
if (INTEGER == DemoNumber) {
break;
System.out.println("The number is: " + DemoNumber);
}
}
}
}
The output for this is:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unreachable code
at delftstack.Unreachable_Statement.main(Unreachable_Statement.java:12)
Using continue:
package delftstack;
public class Unreachable_Statement {
public static void main(String... args) {
int[] DemoArray = {350, 780, 300, 500, 120, 1024, 1350};
int DemoNumber = 1024;
for (int INTEGER : DemoArray) {
if (INTEGER == DemoNumber) {
continue;
System.out.println("The number is: " + DemoNumber);
}
}
}
}
The output for this will also be the same:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unreachable code
at delftstack.Unreachable_Statement.main(Unreachable_Statement.java:12)
Using return:
package delftstack;
public class Unreachable_Statement {
public static void main(String... args) {
int[] DemoArray = {350, 780, 300, 500, 120, 1024, 1350};
int DemoNumber = 1024;
for (int INTEGER : DemoArray) {
if (INTEGER == DemoNumber) {
return;
System.out.println("The number is: " + DemoNumber);
}
}
}
}
The output is:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unreachable code
at delftstack.Unreachable_Statement.main(Unreachable_Statement.java:12)
Solve the unreachable statement Error in Java
The solution is to avoid writing any code immediately after the branching statements. See the code solution for the error raised using the break statement:
package delftstack;
public class Unreachable_Statement {
public static void main(String... args) {
int[] DemoArray = {350, 780, 300, 500, 120, 1024, 1350};
int DemoNumber = 500;
boolean FoundNumber = false;
for (int INTEGER : DemoArray) {
if (INTEGER == DemoNumber) {
FoundNumber = true;
break;
}
}
if (FoundNumber) {
System.out.println("The number is: " + DemoNumber);
}
}
}
We have put the statement in another if condition. The code will now work properly.
See output:
Similarly, we can create a solution for continue and return statements. The rule is not to put any code immediately after the branching statements.