Why Does Java Have an "Unreachable Statement" Compiler Error

Why does Java have an unreachable statement compiler error?

Because unreachable code is meaningless to the compiler. Whilst making code meaningful to people is both paramount and harder than making it meaningful to a compiler, the compiler is the essential consumer of code. The designers of Java take the viewpoint that code that is not meaningful to the compiler is an error. Their stance is that if you have some unreachable code, you have made a mistake that needs to be fixed.

There is a similar question here: Unreachable code: error or warning?, in which the author says "Personally I strongly feel it should be an error: if the programmer writes a piece of code, it should always be with the intention of actually running it in some scenario." Obviously the language designers of Java agree.

Whether unreachable code should prevent compilation is a question on which there will never be consensus. But this is why the Java designers did it.


A number of people in comments point out that there are many classes of unreachable code Java doesn't prevent compiling. If I understand the consequences of Gödel correctly, no compiler can possibly catch all classes of unreachable code.

Unit tests cannot catch every single bug. We don't use this as an argument against their value. Likewise a compiler can't catch all problematic code, but it is still valuable for it to prevent compilation of bad code when it can.

The Java language designers consider unreachable code an error. So preventing it compiling when possible is reasonable.


(Before you downvote: the question is not whether or not Java should have an unreachable statement compiler error. The question is why Java has an unreachable statement compiler error. Don't downvote me just because you think Java made the wrong design decision.)

Why does a Java Compiler not produce an unreachable statement error for an unreachable then statement?

The behaviour is defined in the JLS description of unreachable statements:

The then-statement is reachable iff the if-then statement is reachable.

So the compiler determines that the then-statement (break;) is reachable, regardless of the condition in the if.

And a bit further, emphasis mine:

A basic for statement can complete normally iff at least one of the following is true:

  • The for statement is reachable, there is a condition expression, and the condition expression is not a constant expression (§15.28) with value true.
  • There is a reachable break statement that exits the for statement.

So the for can complete normally because the then-statement contains a break. As you noticed, it would not work if you replaced break with return.


The rationale is explained towards the end of the section. In substance, if has a special treatment to allow constructs such as:

if(DEBUG) { ... }

where DEBUG may be a compile time constant.

Why am I getting an Unreachable Statement error in Java?

You were right assuming that your problem is here:

return theText.toString();
System.out.println(theText);

the return function will terminate your method, meaning no line of code past it will be executed.
If you want your print to go through, you should move it above the return statement.

Unreachable statement is a kind of error instead of warning in Java?

Yes, unreachable code is a compilation error in Java, not just a warning. The code inspection does not "look" into conditionals, so you can simply do the following:

if (true)
throw new MyException();

Unreachable code compiling without error - How?

I believe these are the relevant quotes from JLS 14.21:

  • An empty block that is not a switch block can complete normally iff it is reachable.

    A non-empty block that is not a switch block can complete normally iff the last statement in it can complete normally.

    The first statement in a non-empty block that is not a switch block is reachable iff the block is reachable.

    Every other statement S in a non-empty block that is not a switch block is reachable iff the statement preceding S can complete normally.

So your

System.out.println("I am unreachable??!!!");

statement is reachable iff (that means "if and only if") the try statement can complete normally, which leads to the next quote:

  • A try statement can complete normally iff both of the following are true:

    • The try block can complete normally or any catch block can complete normally.

    • If the try statement has a finally block, then the finally block can complete normally.

Since your catch block can complete normally and you have a finally block that can complete normally, the try statement can complete normally. Hence the System.out.println("I am unreachable??!!!"); statement following it is deemed reachable, regardless of the return; statement inside the try block.

Note the or in

The try block can complete normally or any catch block can complete normally.

This requires either the try block or at least one of the catch blocks to complete normally. It doesn't require both the try block and catch block to complete normally.

Finally, the logic behind this behavior:

The compiler is not supposed to analyze whether a try block can or cannot throw an Exception. The reason is that the Exception class hierarchy includes both checked and unchecked exceptions, and unchecked exceptions are not declared in throws clauses (if you replaced Exception with some checked exception, such as IOException, the compiler would complain that your try block never throws that exception, which would make the catch block unreachable).

Therefore, since you have a catch (Exception e) block which can complete normally, the compiler assumes that this catch block it reachable, and therefore the entire try statement can complete normally, even though the try block cannot complete normally.

The finally block, if present, must also be able to complete normally, since the finally block is also executed, so if it couldn't complete normally, the entire try statement couldn't complete normally.

What is the meaning of Unreacheable Statement

The statement is logically unreachable due to some your program's control
flow. You are throwing an exception that isn't caught within your function.

Please delete the throw statement and the word "Template"

   public class parseMETHOD {
public static void main(String[] args) {
int a=9;
int b=45;
int result=calFunction(a,b);
System.out.println(result);
}

private static int calFunction(int t,int r) {

int addition = t+r;
return addition;

}
}

Also, good naming conventions state that you should capitalize class names.

Unreachable Statement in Java

If, this, is really the method in your code:

public String seeDVDInfo() {
return filmName;
return director;
return leadActor;
}

Then, the problem is that a method is finished with the a return statement. It will exit the method with the provided value.

You have different ways to solve the issue, depending on the information you want to provide/use:

 public String seeDirector() {
return director;
}
public String seeFilmName() {
return director;
}
public String leadActor() {
return director;
}

Taking into account that java is an OOP language, I would suggest creating a class Film that contains/ encapsulates this info that can be returned. So actually it will look something like this:

public class Film {
private String director;
private String filmName;
private String leadActor;

public Film(String director, String filmName, String leadActor) {
this.director = director;
this.filmName = filmName;
this.leadActor = leadActor;
}

public String getFilmName() {
return filmName;
}

public void setFilmName(String filmName) {
this.filmName = filmName;
}

public String getDirector() {
return director;
}

public void setDirector(String director) {
this.director = director;
}
public String getLeadActor() {
return leadActor;
}

public void setLeadActor(String leadActor) {
this.leadActor = leadActor;
}
}

Unreachable statement: while true vs if true

There are quite strict rules when statements are reachable in java. These rules are design to be easily evaluated and not to be 100% acurate. It should prevent basic programming errors. To reason about reachability in java you are restricted to these rules, "common logic" does not apply.

So here are the rules from the Java Language Specification 14.21. Unreachable Statements

An if-then statement can complete normally iff it is reachable.

So without an else, statements after an if-then are always reachable

A while statement can complete normally iff at least one of the following is true:

  • The while statement is reachable and the condition expression is not a constant expression (§15.28) with value true.

  • There is a reachable break statement that exits the while statement.

The condition is a constant expression "true", there is no break. Hence it does not complete normally.



Related Topics



Leave a reply



Submit