Unreachable Code Error in Java
The term "Unreachable Statements" refers to statements that won't be performed while the program is running. When the code can't be compiled for a number of factors, it results in an unreachable code error. The following are the reasons that results in unreachable code or statements:
- Statements after the return Statement
- Statements after the break statement
- Statements after the infinite loop
- Any statements in a try block that follow an exception-throwing statement.
Case 1: Statements after the return Statement
When a return statement is used, the execution of that function is immediately terminated. Any further statements won't be carried out as a result. The error code is unreachable as a result.
FileName: UnreachableCode1.java
import java.io.*; import java.util.*; public class UnreachableCode1 { public static void main(String[] args) { System.out.println(foo(100)); } // foo() method creation public static int foo(int x) { return 5; // It will never execute and // returns an error for unreachable code. System.out.println("Inside a Method foo()"); } }
Output:
UnreachableCode1.java:15: error : unreachable statement System.out.println("Inside a Method foo()"); ^ 1 error
Case 2: Statements after the break statement
The "break" statement is used when combined with the decision-making statements and loops. If the condition needed is attained, it ends the execution cycle. The line after the "break" statement in that block is not reached by the compiler.
FileName: UnreachableCode2.java
import java.io.*; import java.util.*; public class UnreachableCode2 { public static void main(String[] args) { for(int i=1;i<10;i++) { System.out.println(i); break; // It will never execute and // returns an error for unreachable code. System.out.println("Following Break Statement..."); } } }
Output:
UnreachableCode2.java:11: error: unreachable statement System.out.println("Following Break Statement..."); ^ 1 error
Case 3: Statements after the Infinite Loop
While a specific condition is true, the while loop statement repeatedly runs (loops through) a code block. The statement(s) inside the while loop will never be performed if this condition can't be satisfied before starting the loop. Due to this, the compiler will run into the unreachable statement error.
FileName: UnreachableCode4.java
import java.io.*; import java.util.*; public class UnreachableCode4 { public static void main(String[] args) { // declare and intialization of variables 'x' and 'y' // using final keyword final int x=100; final int y=200; while(x>y) { System.out.println("Execute Inside the while block"); } System.out.println("Execute Outside while block"); } }
Output:
UnreachableCode4.java:10: error: unreachable statement { ^ 1 error
Case 4: Any statements in a try block that follow an exception-throwing statement
After throwing an exception,if we add any statements to a try-catch block, those sentences will not be available since execution will move to the catch block or finally block due to the exceptional occurrence. The lines that follow the throw are not carried out.
FileName: UnreachableCode5.java
import java.io.*; import java.util.*; public class UnreachableCode5 { public static void main(String args[]) { try { throw new Exception("Custom Exceptions"); // It will never execute and // returns an error for unreachable code. System.out.println("Hello World"); } catch (Exception exe) { // calling with an exception with an object exe.printStackTrace(); } } }
Output:
UnreachableCode5.java:8: error: unreachable statement System.out.println("Hello World"); ^ 1 error