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.)
Introduction to Statements and Compile-time Errors in Java
Statements are foundational language constructs that have an effect on the execution of a program. Statements are similar to sentences in natural languages. In Java, there are three main types of statements, namely expression statements, declaration statements, and control-flow statements [1].
As a compiled programming language, Java has an inbuilt mechanism for preventing many source code errors from winding up in executable programs and surfacing in production environments [2]. One such error, related to statements, is the unreachable statement
error.
What Causes the Unreachable Statement Error?
By performing semantic data flow analysis, the Java compiler checks that every statement is reachable and makes sure that there exists an execution path from the beginning of a constructor, method, instance initializer, or static initializer that contains the statement, to the statement itself. If it finds a statement for which there is no such path, the compiler raises the unreachable statement
error [3].
Unreachable Statement Error Examples
After a branching control-flow statement
The break
, continue
, and return
branching statements allow the flow of execution to jump to a different part of the program. The break
statement allows breaking out of a loop, the continue
statement skips the current iteration of a loop, and the return
statement exits a method and returns the execution flow to where the method was invoked [4]. Any statement that follows immediately after a branching statement is, by default, unreachable.
After break
When the code in Fig. 1(a) is compiled, line 12 raises an unreachable statement
error because the break
statement exits the for
loop and the successive statement cannot be executed. To address this issue, the control flow needs to be restructured and the unreachable statement removed, or moved outside the enclosing block, as shown in Fig. 1(b).
(a)
package rollbar;
public class UnreachableStatementBreak {
public static void main(String... args) {
int[] arrayOfInts = {35, 78, 3, 589, 12, 1024, 135};
int searchFor = 12;
for (int integer : arrayOfInts) {
if (integer == searchFor) {
break;
System.out.println("Found " + searchFor);
}
}
}
}
UnreachableStatementBreak.java:12: error: unreachable statement
System.out.println("Found " + searchFor);
^
(b)
package rollbar;
public class UnreachableStatementBreak {
public static void main(String... args) {
int[] arrayOfInts = {35, 78, 3, 589, 12, 1024, 135};
int searchFor = 12;
boolean found = false;
for (int integer : arrayOfInts) {
if (integer == searchFor) {
found = true;
break;
}
}
if (found) {
System.out.println("Found " + searchFor);
}
}
}
Found 12
After continue
Just as with the break
statement, any statement following a continue
statement will result in an unreachable statement
error. The continue
statement on line 12 in Fig. 2(a) halts the current iteration of the for
loop and forwards the flow of execution to the subsequent iteration, making the print statement unreachable. Placing the print statement in front of the continue
statement resolves the issue (Fig. 2(b)).
(a)
package rollbar;
public class UnreachableStatementContinue {
public static void main(String... args) {
int[] arrayOfInts = {35, 78, 3, 589, 12, 1024, 135};
int oddSum = 0;
for (int integer : arrayOfInts) {
if (integer % 2 == 0) {
continue;
System.out.println("Skipping " + integer + " because it's even");
}
oddSum += integer;
}
System.out.println("nThe sum of the odd numbers in the array is: " + oddSum);
}
}
UnreachableStatementContinue.java:12: error: unreachable statement
System.out.println("Skipping " + integer + " because it's even");
^
(b)
package rollbar;
public class UnreachableStatementContinue {
public static void main(String... args) {
int[] arrayOfInts = {35, 78, 3, 589, 12, 1024, 135};
int oddSum = 0;
for (int integer : arrayOfInts) {
if (integer % 2 == 0) {
System.out.println("Skipping " + integer + " because it's even");
continue;
}
oddSum += integer;
}
System.out.println("nThe sum of the odd numbers in the array is: " + oddSum);
}
}
Skipping 78 because it's even
Skipping 12 because it's even
Skipping 1024 because it's even
The sum of the odd numbers in the array is: 762
After return
The return
statement on line 10 in Fig. 3 unconditionally exits the factorial
method. Therefore, any statement within this method that comes after the return
statement cannot be executed, resulting in an error message.
(a)
package rollbar;
public class UnreachableStatementReturn {
static int factorial(int n) {
int f = 1;
for (int i = 1; i <= n; i++) {
f *= i;
}
return f;
System.out.println("Factorial of " + n + " is " + f);
}
public static void main(String... args) {
factorial(10);
}
}
UnreachableStatementReturn.java:11: error: unreachable statement
System.out.println("Factorial of " + n + " is " + f);
^
(b)
package rollbar;
public class UnreachableStatementReturn {
static int factorial(int n) {
int f = 1;
for (int i = 1; i <= n; i++) {
f *= i;
}
return f;
}
public static void main(String... args) {
int n = 10;
System.out.println("Factorial of " + n + " is " + factorial(n));
}
}
Factorial of 10 is 3628800
After a throw statement
An exception is an event that occurs during the execution of a program and disrupts the normal flow of instructions. For an exception to be caught, it has to be thrown by some code, which can be any code, including code from an imported package, etc. An exception is always thrown with the throw
statement [5].
Any statement added to a block after throwing an exception is unreachable and will trigger the unreachable statement
error. This happens because the exception event forces the execution to jump to the code catching the exception, usually a catch
clause within a try-catch
block in the same or a different method in the call stack. See Fig. 4 for an example.
(a)
package rollbar;
public class UnreachableStatementException {
public static void main(String... args) {
try {
throw new Exception("Custom exception");
System.out.println("An exception was thrown");
} catch (Exception e) {
System.out.println(e.getMessage() + " was caught");
}
}
}
UnreachableStatementException.java:7: error: unreachable statement
System.out.println("An exception was thrown");
^
(b)
package rollbar;
public class UnreachableStatementException {
public static void main(String... args) {
try {
throw new Exception("Custom exception");
} catch (Exception e) {
System.out.println("An exception was thrown");
System.out.println(e.getMessage() + " was caught");
}
}
}
An exception was thrown
Custom exception was caught
Inside a while loop with invariably false condition
The while
statement continually executes (loops through) a code block while a particular condition evaluates to true
. If this condition can’t be met prior to entering the while
loop, the statement(s) inside the loop will never be executed. This will cause the compiler to invoke the unreachable statement
error (Fig. 5).
package rollbar;
public class UnreachableStatementWhile {
public static void main(String... args) {
final boolean start = false;
while (start) {
System.out.println("Unreachable Statement");
}
}
}
UnreachableStatementWhile.java:7: error: unreachable statement
while (start){
^
Summary
This article helps understand, identify, and resolve the unreachable statement
error, which is a frequently encountered compile-time semantic error in Java. This error stems from irregularities in the execution flow of a program, where certain statements are unreachable by any means, i.e., none of the possible execution paths lead to them. To avoid this error, careful design and examination of the execution flow of a program is advisable.
Track, Analyze and Manage Errors With Rollbar
Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing Java errors easier than ever. Sign Up Today!
References
[1] Oracle, 2021. Expressions, Statements, and Blocks (The Java™ Tutorials > Learning the Java Language > Language Basics). Oracle and/or its affiliates. [Online]. Available: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/expressions.html . [Accessed Nov. 10, 2021].
[2] Rollbar, 2021. Illegal Start of Expression: A Java Compile-time Errors Primer. Rollbar Editorial Team. [Online]. Available: https://rollbar.com/blog/how-to-fix-illegal-start-of-expression-in-java/. [Accessed Nov. 10, 2021].
[3] Oracle, 2021. The Java® Language Specification. Chapter 14. Blocks, Statements, and Patterns. Unreachable Statements. Oracle and/or its affiliates. [Online]. Available: https://docs.oracle.com/javase/specs/jls/se17/html/jls-14.html#jls-14.22. [Accessed Nov. 10, 2021].
[4] Oracle, 2021. Branching Statements (The Java™ Tutorials > Learning the Java Language > Language Basics). Oracle and/or its affiliates. [Online]. Available: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html. [Accessed Nov. 10, 2021].
[5] Oracle, 2021. How to Throw Exceptions (The Java™ Tutorials > Essential Java Classes > Exceptions). Oracle and/or its affiliates. [Online]. Available: https://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html. [Accessed Nov. 10, 2021].
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
/*
Не компилируется задача про котиков
*/
public class Solution {
public final static ArrayList<Cat> CATS = new ArrayList<>();
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
String name = reader.readLine();
if (name.isEmpty());
int age = Integer.parseInt(reader.readLine());
int weight = Integer.parseInt(reader.readLine());
int tailLength = Integer.parseInt(reader.readLine());
Cat cat = new Cat(name, age, weight, tailLength);
CATS.add(cat);
}
printList(); // говорит, что это недостижимый код (UNREACHABLE STATEMENT)
}
public static void printList() {
for (int i = 0; i < CATS.size(); i++) {
System.out.println(CATS.get(i));
}
}
public static class Cat {
private String name;
private int age;
private int weight;
private int tailLength;
Cat(String name, int age, int weight, int tailLength) {
this.name = name;
this.age = age;
this.weight = weight;
this.tailLength = tailLength;
}
@Override
public String toString() {
return "Cat's name: " + name + ", age: " + age + ", weight: " + weight + ", tail: " + tailLength;
}
}
}
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!!
Improve Article
Save Article
Like Article
Improve Article
Save Article
Like 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
Last Updated :
22 Oct, 2020
Like Article
Save Article