同时(真);当不在 void 中时,循环会抛出无法访问的代码 [英] while(true); loop throws Unreachable code when isn't in a void

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

问题描述

我在用java做一些小程序.我知道如果我写 while(true); 程序将在这个循环中冻结.如果代码是这样的:

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:

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

编译器向我抛出错误:

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

我不知道这个错误存在.但我知道为什么会抛出它.当然,第 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:

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() 的内部并发现它不可访问.可以肯定的是,我尝试过:

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:

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

结果与测试 2 相同.

经过一些研究,我发现了这个问题.所以,如果括号内的代码是一个变量,编译器就不会抛出异常.这是有道理的,但我不认为这同样适用于 voids.

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.

问: 那么,如果 void b()(测试 2),为什么编译器会在测试 1 中向我抛出错误并且 System.out.println("end");(测试 3)无法访问?

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?

我在 C++ 中尝试了测试 1:

I tried Test 1 in C++:

#include <iostream>

using namespace std;

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

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

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?

推荐答案

语言规范有一个确切的定义编译器应该将什么视为无法访问的代码,另见https://stackoverflow.com/a/20922409/14955.

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.

它不会做更多.

但是,您可以使用 FindBugs 等静态代码分析工具进行更深入"的分析(但不确定它们是否检测到您描述的模式,并且正如其他人指出的那样,在无论如何,所有的一般性都无法通过算法解决,因此必须在尽力而为"的某个合理定义上划清界限).

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").

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

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