Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

while(true); loop throws Unreachable code when isn't in a void

I was doing some small programs in java. I know that if I write while(true); the program will freeze in this loop. If the code is like that:

Test 1:

public class While {     public static void main(String[] args) {         System.out.println("start");         while (true);         System.out.println("end");     } } 

The compiler throws me the error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:      Unreachable code     at While.main(While.java:6) 

I didn't know that this error exists. But I got why it is thrown. Of course, line 6 was unreachable, causing a compilation problem. Then I tested this:

Test 2:

public class While {     public static void main(String[] args) {         System.out.println("start");         a();         b();     }     static void a() {         while(true);     }     static void b() {         System.out.println("end");     } } 

For some reason the program ran normally (The console printed "start" and then froze). The compiler couldn't check inside of void a() and see that it isn't reachable. To be sure I tried:

Test 3:

public class While {     public static void main(String[] args) {         System.out.println("start");         a();         System.out.println("end");     }     static void a() {         while(true);     } } 

Same result as Test 2.

After some research I found this question. So, if the code inside the parentheses is a variable the compiler wouldn't throw the exception. That makes sense, but I don't think that the same applies to voids.

Q: So, why does the compiler just throw me the error at Test 1, if void b() (Test 2) and System.out.println("end"); (Test 3) isn't reachable?

EDIT: I tried Test 1 in C++:

#include <iostream>  using namespace std;  int main() {     cout << "start" << endl;     while(true);     cout << "end" << endl;     return 0; } 

The compiler didn't throw any errors, then I got the same result as Test 2 and as Test 3. So I suppose this is a java thing?

like image 430
A Cat Avatar asked Jun 09 '14 23:06

A Cat


People also ask

Why are statements unreachable?

The Unreachable statements refers to statements that won't get executed during the execution of the program are called Unreachable Statements. These statements might be unreachable because of the following reasons: Have a return statement before them. Have an infinite loop before them.

Why is break statement unreachable?

After break 1(a) is compiled, line 12 raises an unreachable statement error because the break statement exits the for loop and the successive statement cannot be executed.


1 Answers

The language spec has an exact definition what the compiler should treat as unreachable code, see also https://stackoverflow.com/a/20922409/14955.

In particular, it does not care about if a method completes, and it does not look inside other methods.

It won't do more than that.

However, you could get static code analysis tools like FindBugs to get a "deeper" analysis (not sure if they detect the pattern you described, though, either, and, as has been pointed out by others, the halting problem in all generality cannot be algorithmically solved anyway, so one has to draw the line at some reasonably definition of "best effort").

like image 159
Thilo Avatar answered Oct 06 '22 00:10

Thilo