0

I use eclipse IDE for java developers

EDIT: i should be referring only to eclispe IDE as other compilers for different languages only give warings and can handle the code

I understand what will cause this error(unreachable code), but why does it exist as an error not a warning? What is it in the compiler that can't handle unreachable code?

Unreachable code: error or warning? I have seen this question but it doesn't answer why a compiler can't handle unreachable and why it has to give an error.

From what i think surely it can compile it and just never reach the code in the compiled program but obviously compilers (i use eclipse for java) don't allow it.

Community
  • 1
  • 1
  • Java is the language that a lot of people learn first, at least nowadays. Perhaps the designers had this in mind and decided to restrict people from having unreachable code even if it doesn't actually hurt. Might as well teach these programming-newbies to not have unreachable code. Newbies perhaps tend to ignore warnings since they are so excited to finish their task/project. – RaminS Feb 26 '16 at 19:51
  • Your question seems to be based on false assumptions. If you look at [this question](http://stackoverflow.com/questions/30457156/why-unreachable-code-isnt-an-error-in-c) you will see that indeed in C/C++ dead code is a warning and the compiler is able to handle it. (Handle here means either eliminate it or ignore it) – Anedar Feb 26 '16 at 19:52
  • BTW this is a design decision, not a technical one; is not that the compiler "can't handle it" it's that Java compiler design tries to avoid warnings at all as they are typically ignored by the user. Before generics were introduced there were no warnings at all. Now it emits some warnings if the semantics of generics are ignored. – OscarRyz Feb 26 '16 at 20:19

1 Answers1

1

Simply put, if they can't, it's because they were designed not to.

Most would agree that it should simply be a warning. The reason Java does not treat this as a warning is because it was designed to be an error. From the Java Language Specification for J7SE:

"It is a compile-time error if a statement cannot be executed because it is unreachable."

Some languages will only throw warnings (i.e., C#), but Java was designed otherwise.

TayTay
  • 6,882
  • 4
  • 44
  • 65
  • 1
    It bears mentioning that Eclipse let's you configure this to be a warning instead of an error, if you're so inclined. I don't recommend it, but it's possible. Thus, the compiler *can* "handle" it. – E-Riz Feb 26 '16 at 21:07