How to Solve the Unreachable Statement Error 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:
The number is: 500
Similarly, we can create a solution for continue
and return
statements. The rule is not to put any code immediately after the branching statements.
Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.
LinkedIn FacebookRelated Article - Java Error
- How to Fix the Error: Failed to Create the Java Virtual Machine
- How to Fix the Missing Server JVM Error in Java
- How to Fix the 'No Java Virtual Machine Was Found' Error in Eclipse
- How to Fix Javax.Net.SSL.SSLHandShakeException: Remote Host Closed Connection During Handshake
- How to Fix the Error: Failed to Create the Java Virtual Machine
- How to Fix Java.Lang.VerifyError: Bad Type on Operand Stack