Unreachable code compiler error

ughzan :

The following code gives an unreachable statement compiler error

public static void main(String[] args) {
    return;
    System.out.println("unreachable");
}

Sometimes for testing purposes a want to prevent a method from being called, so a quick way to do it (instead of commenting it out everywhere it's used) is to return immediately from the method so that the method does nothing. What I then always do to get arround the compiler error is this

public static void main(String[] args) {
    if (true) {
        return;
    }
    System.out.println("unreachable");
}

I'm just curious, why is it a compiler error?? Will it break the Java bytecode somehow, is it to protect the programmer or is it something else?

Also (and this to me is more interesting), if compiling java to bytecode does any kind of optimization (or even if it doesn't) then why won't it detect the blatant unreachable code in the second example? What would the compiler pseudo code be for checking if a statement is unreachable?

André :

Unreachable code is meaningless, so the compile-time error is helpful. The reason why it won’t be detected at the second example is, like you expect, for testing / debugging purposes. It’s explained in The Specification:

if (false) { x=3; }

does not result in a compile-time error. An optimizing compiler may realize that the statement x=3; will never be executed and may choose to omit the code for that statement from the generated class file, but the statement x=3; is not regarded as "unreachable" in the technical sense specified here.

The rationale for this differing treatment is to allow programmers to define "flag variables" such as:

static final boolean DEBUG = false;

and then write code such as:

if (DEBUG) { x=3; }

The idea is that it should be possible to change the value of DEBUG from false to true or from true to false and then compile the code correctly with no other changes to the program text.

Reference: http://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.21

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Code Unreachable Error in Code As Warnng

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

How does the compiler in Java decide on unreachable code?

compiler tries to evaluate unreachable code after constexpr if

Java unreachable catch block compiler error

Why does compiler build return unreachable code in some cases

Why does Java have an "unreachable statement" compiler error?

Unreachable code compiling without error - How?

Dialogflow is giving error unreachable code after return

how to solve error :This code is unreachable - in pycharm

SQL Server gives error to unreachable code

Unreachable code, compilation error vicious circle

Unreachable Code - Shouldn't be unreachable

Why is an if/else if/else for a simple boolean not giving an "unreachable code" error

Why unreachable code isn't an error in C++?

Make "Unreachable code detected" compilation warning instead of error in TypeScript?

Why do I get an "unreachable code" and "variable not initialized" compilation error?

Error: Unreachable code detected ionic 3 weather app

How do I solve the "unreachable code detected" error in "if" statement?

Is this an ARM Compiler code generation error?

Why this is not an unreachable statement for the Java compiler?

Compiler Error Message: The compiler failed with error code -532462766

Compiler Error Message: The compiler failed with error code -2146232576

Exception Handling Unreachable code

Is `with return .. return` unreachable code?

Unreachable code, but reachable with an exception

PyCharm: Unreachable Code?

TypeScript produces unreachable code

unreachable code detected on for loop