Javatpoint Logo
Javatpoint Logo

Unreachable Code or Statement in Java

An unreachable code or statement in Java is a common problem among Java beginners. It is a compile-time error. A lot of novice developers confuse the error with dead code- another Java-related phenomenon. Although the two are similar by manifestation, there are a slight difference between two that we will discuss in this section.

Unreachable Code or Statement in Java

Other than that, we will find that the most common reasons for compiler returning an unreachable code statement are and discover some easy fixes to get code and running again.

What is Unreachable code?

By definition, an unreachable statement is a statement that will not be executed by a compiler when we execute ready-to-deploy code. An unreachable code return statement is typically a sign of a logical error within the program. Although there are several reasons why we end up with such a statement, in all cases, unreachable code is redundant, clutters of the program, and should be avoided at all costs. It can happen for various reasons, such as:

  • A return statement before the unreachable statement.
  • An infinite loop before the unreachable statement.
  • An exception thrown before the unreachable statement.
  • A break or continue statement before the unreachable statement.

Why should we avoid unreachable statements?

There are a few reasons why we should avoid unreachable statements in Java code:

  • It can make code more difficult to read and maintain.
  • It can be a sign of a logical error in code.
  • It can waste time and resources, as the compiler and interpreter will still have to process them.

Why we get Unreachable code statements?

The good news is, it's easy to trace the cause of unreachable code issues. There are the following three reasons why the compiler keeps returning errors:

  • Transfer statements: If we break code with a return statement, nothing after "return = true" will be executed.
  • Infinite loop: A piece of code will not executed that we have written after an infinite loop. The loop keeps reiterating the loop action again and again. Thus, when converting code in byte code, the compiler will send an unreachable code error.

Return

A return statement is a part of the transfer keyword group, meaning that it terminates method. It is helpful for separating functions and keep code readable and clean.

However, we cannot add new statements to the function after return = true, trying to continue the function after the keyword will give an Unreachable code error.

ReturnStatement.java

Output:

error: unreachable statement
System.out.println("My code will run");
^
1 error

Break Statements

Break statements are another type of keyword. We need to be careful while writing Java functions. By the definition, the break keyword is used to terminate a loop.

In the example below, while we exit from the for loop, we will no longer be able to execute the statement at line 8. Thus, the compiler will show an unreachable statement error.

BreakStatement.java

Output:

error: unreachable statement
System.out.println("After break");
^
1 error

Continue Statements

Continue is a loop control keyword used to reiterate actions. Whenever we want the execution of a loop to start from scratch, add continue to the code. The statement is useful to help developers choose which statements of the loop they want to reiterate and the ones they'd not put in the iteration.

Although continue is a straightforward keyword to use, not having full understanding of how it works leads developers to the unreachable code trap. Since, after encountering a continue, it will reiterate the loop, the keyword will not be able to reach the statements that follow it.

ContinueStatement.java

Output:

error: unreachable statement
System.out.println("Coding after continue");

A scenario that's similar to the examples of "break" and "continue" keyword use cases is that of an infinite loop. When designing an infinite loop, a developer should remember that no statement after it will ever run. Thus, if we do not break the loop, all the code written after will be unreachable.

InfiniteloopStatement.Java

Output:

error: unreachable statement
System.out.println("Nice to see you");
'

Conclusion

To avoid unreachable code in Java, it is important to ensure a flow to all system statements and handle keywords and loops properly. Double-checking the code is the best way to prevent such mistakes.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA