1

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.

Daniel Trebbien
  • 38,421
  • 18
  • 121
  • 193
  • 1
    I think your question should be reworded to ask if you can detect a fall through case statement. Unreachable code doesn't compile – OneCricketeer Sep 29 '17 at 23:20
  • Not sure if I'm getting your question right, but, yes, java compiler will mark this points for you and you will have to correct the code in order to compile it. Though, I don't think that java compiler 100% shows you all the unreachable statements in the code, cause even with one unreachable statement your code is already broken. You can see this unreachable statement compilation errors in any IDE, Eclipse, for example. – Sergei Sirik Sep 29 '17 at 23:25
  • 1
    By the way `fallthrough` is already a javac lint option – OneCricketeer Sep 29 '17 at 23:27
  • @cricket_007 It's not just fallthrough that I am thinking as a potential use case. Another use case is to make sure that if control flow reaches a certain block within a loop, then the end of the block is unreachable; i.e. either all branches within the block break from the loop or return from the function. Also, I am aware of `-Xlint:fallthrough`, but I am intentionally using fallthrough in other places. I suppose that I could, as a partial solution, enable this and suppress the warning (via `@SuppressWarnings("fallthrough")`) where fallthrough is intended. – Daniel Trebbien Sep 30 '17 at 03:27
  • @SergeiSirik I am wondering if there is a way to mark unreachable points in a way that is not a compilation error. – Daniel Trebbien Sep 30 '17 at 03:33
  • I'm not getting this. If the code is correctly unreachable, it won't compile, and if it becomes reachable it will compile. So if it compiles it's wrong. Which isn't of much use. What is this code doing there at all, if it's unreachable? – user207421 Sep 30 '17 at 03:49
  • @EJP It can't be code, or else the compilation unit won't compile. The problem is that, if I modify the code in question and forget to add a `return` statement or break from the loop, then control flow *could* fall through or continue the loop, but that will probably cause problems. What I would like to know is if there is a common way of marking unreachable places in code, like a special comment syntax or something. It would be even better if a tool existed that could automatically verify that the marked spot (i.e. the comment or whatever) is, in fact, unreachable. – Daniel Trebbien Sep 30 '17 at 04:43

1 Answers1

0

I am wondering if there is a way to mark unreachable points in a way that is not a compilation error.

The Java language provides no way to do this ... apart from commenting out the code.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216