while(true);当不在void中时,循环抛出不可到达的代码 [英] while(true); loop throws Unreachable code when isn't in a void

查看:388
本文介绍了while(true);当不在void中时,循环抛出不可到达的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在java里做了一些小程序。我知道如果我写 while(true); 程序将冻结在这个循环。如果代码如下:



测试1:



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

错误:

 线程中的异常mainjava.lang.Error:Unresolved compilation problem: 
无法到达的代码
at While.main(While.java:6)

我不知道这个错误存在。但我得到了为什么它被抛出。当然,第6行无法访问,导致编译问题。然后我测试了这个:



测试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);
}
}

由于某些原因程序正常运行(控制台打印开始,然后冻结)。编译器无法检查 void a() 的内部并且看不到它。确定我试过:



测试3:



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

与测试2相同。 strong>



经过一番研究,我发现这 问题 。因此,如果圆括号内的代码是变量,编译器不会抛出异常。这是有道理的,但我不认为这同样适用于 voids



Q:因此,为什么编译器只是在测试1中抛出错误,如果 void b() (测试2)和 System.out.println(end); (测试3) > #include< iostream>

using namespace std;

int main()
{
cout< start< endl;
while(true);
cout<< end< endl;
return 0;
}

编译器没有抛出任何错误那么我得到与测试2和测试3相同的结果。所以我想这是一个java东西?

解决方案

a href =http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.21>语言规范有一个确切的定义编译器应该如何处理作为无法访问的代码,另请参见 http://stackoverflow.com/a/20922409/14955



特别是,它不关心一个方法是否完成,它不看其他方法。



它不会做更多。



但是,您可以获取静态代码分析工具(如FindBugs)来获得更深入的分析(不知道他们是否检测到您描述的模式,正如其他人已经指出的,所有通用性的停顿问题不能在算法上解决,所以人们必须在最大努力的合理定义上绘制线)。


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?

解决方案

The language spec has an exact definition what the compiler should treat as unreachable code, see also http://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").

这篇关于while(true);当不在void中时,循环抛出不可到达的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆