Per the Java Language Specification Java SE 8 Edition, §14.21 Unreachable Statements:
It is a compile-time error if a statement cannot be executed because it is unreachable.
The Standard goes on to define precisely what reachable/unreachable and can complete normally mean.
Therefore, one cannot place a statement like assert false;
or throw new RuntimeException("can't reach here");
at an unreachable point in code because this will cause a compilation error.
Is there a standard or common practice way of marking an unreachable point in code without introducing a compilation error, such as a comment with some special syntax (e.g. //unreachable
)? Hopefully, there also exist lint tools that can automatically verify that these markings indeed correspond to unreachable points, similar to this question asking about special comments in a certain C codebase.
The reason I am asking is because I have a piece of code containing a lengthy chain of if/else if/else blocks within a switch case that each (currently) end with a return
statement. However, I am anticipating that another conditional block might be added which I will forget to end with a return
statement, causing unintentional fall-through in the switch
statement. Another potential use is to make sure that a certain conditional block of code will always break from a loop.