Unreachable Code Error in Java

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

filter_none

edit
close

play_arrow

link
brightness_4
code

class GFG {
    public static void main(String args[])
    {
 
        System.out.println("I will get printed");
 
        return;
 
        // it will never run and gives error
        // as unreachable code.
        System.out.println("I want to get printed");
    }
}

chevron_right


  • 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

filter_none

edit
close

play_arrow

link
brightness_4
code

class GFG {
    public static void main(String args[])
    {
        int a = 2;
        for (;;) {
 
            if (a == 2)
            {
                break;
                // it will never execute, so
                // same error will be there.
                System.out.println("I want to get printed");
            }
        }
    }
}

chevron_right


  1. 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

filter_none

edit
close

play_arrow

link
brightness_4
code

class GFG {
    public static void main(String args[])
    {
 
        try {
            throw new Exception("Custom Exception");
            // Unreachable code
            System.out.println("Hello");
        }
        catch (Exception exception) {
            exception.printStackTrace();
        }
    }
}

chevron_right


  • 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

filter_none

edit
close

play_arrow

link
brightness_4
code

class GFG {
    public static void main(String args[])
    {
        for (int i = 0; i < 5; i++)
        {
            continue;
            System.out.println("Hello");
        }
    }
}

chevron_right


  • Compile Errors: 

prog.java:6: error: unreachable statement 
System.out.println(“Hello”); 

1 error 

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Hello friends, I have a deep knowledge on programming language like C,C++,Java,especially in C and Java I like to do comparative study and likes to ask question ie if , why and why not we In future, i want to be an entrepreneur and really focus on how to improve Education System

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : smithasm7, spamerzinc