Try it here
Subscribe
Java unreachable code

Unreachable Code Error in Java

unreachable_code_error_in_java

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

Scenarios where this error can occur:

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

    class dEe {
        public static void main(String args[])
        {
      
            System.out.println("It will be printed");
      
            return;
      
            // it will never run and gives error
            // as unreachable code.
            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

  2. 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:

    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");
                }
            }
        }
    }

    Compile Errors:

    prog.java:13: error: unreachable statement
    System.out.println(“I want to get printed”);
    ^
    1 error


If you like dEexams.com and would like to contribute, you can also write your article here or mail your article to admin@deexams.com . See your article appearing on the dEexams.com main page and help others to learn.


Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Python if , elif and else

Python Conditions and If statements

  • 0
Python for beginners

Learning Python Part 1

  • 3
Struct Alignment and Padding

Struct Alignment and Padding in C++ And C

  • 0
Friend function

Friend function C++

  • 0
Pointers

C++ Pointers

  • 0
Structures

C++ Structures

  • 0
Types of Inheritance in C++

Inheritance and access specifiers C++

  • 0
Java date pattern

Java Date Pattern Syntax

  • 0
Java Date and Calendar

Java Date formats

  • 0
JAVA Data Type

Data types in Java

  • 0
Java unreachable code

Unreachable Code Error in Java

  • 0

Post Comment

Comments(0)

WEB TECHNOLOGY

Articles

×

Forgot Password

Please enter your email address below and we will send you information to change your password.