Java中无法访问的语句编译错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18308159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unreachable statement compile error in Java
提问by UnderDog
class For1
{
public static void main(String args[])
{
int a = 0;
for(;;)
{
break;
System.out.println(a); //Line 1
++a;//Line 2
}
}
}
I know that Line 1/Line 2 will never be executed. But still I don't understand why a compile time error is thrown. I am getting "unreachable statement" compile error.
我知道第 1 行/第 2 行永远不会被执行。但我仍然不明白为什么会抛出编译时错误。我收到“无法访问的语句”编译错误。
Does it mean that compiler checks whether it is able to compile for all branches/lines of code ?
这是否意味着编译器会检查它是否能够为所有分支/代码行进行编译?
回答by Mario Rossi
The compiler is also able to make that conclusion, and assumes you are making a mistake. And yes, the Java compiler does a pretty good amount of "Data-Flow Analysis". The most common related message is the one about variables not initialized. The second most frequent is, I believe, precisely this one, about code not reachable.
编译器也能够得出这个结论,并假设你犯了一个错误。是的,Java 编译器做了大量的“数据流分析”。最常见的相关消息是关于变量未初始化的消息。我相信,第二个最常见的是关于无法访问的代码。
回答by Jon Skeet
Does it mean that compiler checks whether it is able to compile for all branches/lines of code ?
这是否意味着编译器会检查它是否能够为所有分支/代码行进行编译?
It means the compiler checks that every statement is reachable.
这意味着编译器会检查每个语句是否可达。
From section 14.21 of the JLS:
It is a compile-time error if a statement cannot be executed because it is unreachable.
This section is devoted to a precise explanation of the word "reachable." The idea is that there must be some possible execution path from the beginning of the constructor, method, instance initializer, or static initializer that contains the statement to the statement itself. The analysis takes into account the structure of statements.
如果由于无法访问语句而无法执行语句,则会出现编译时错误。
本节致力于精确解释“可达”一词。这个想法是从包含语句的构造函数、方法、实例初始值设定项或静态初始值设定项的开始到语句本身必须有一些可能的执行路径。分析考虑了报表的结构。
The section then documents how reachability is defined.
然后,该部分记录了如何定义可达性。
In particular, the relevant points in your case are:
特别是,您的案例中的相关要点是:
Every other statement S in a non-empty block that is not a switch block is reachable iff the statement preceding S can complete normally.
A
break
,continue
,return
, orthrow
statement cannot complete normally.
非空块(不是 switch 块)中的每个其他语句 S 都是可访问的,如果 S 之前的语句可以正常完成。
一
break
,continue
,return
,或throw
语句无法正常完成。
So your "line 1" statement is preceded by a statement (break;
) which cannotcomplete normally, and therefore it's unreachable.
因此,您的“第 1 行”语句前面是无法正常完成的语句 ( break;
) ,因此无法访问。
回答by npinti
The compiler will check if there is more code after certain keywords. Another keyword which will cause a similar message is if you replace break
by return
.
编译器会检查某些关键字后是否还有更多代码。另一个会导致类似消息的关键字是如果您替换break
为return
。
回答by Subhrajyoti Majumder
Does it mean that compiler checks whether it is able to compile for all branches/lines of code ?
这是否意味着编译器会检查它是否能够为所有分支/代码行进行编译?
Yes compiler compiles the whole body of code and make byte code according to your code, it smarter enough to detects unreachable code
also dead code
. Immediate break
in the for-loop
makes unreachable other statements.
是的,编译器会编译整个代码体并根据您的代码生成字节码,它unreachable code
也足够聪明地检测dead code
. 立即break
在for-loop
使其他语句无法访问。
for(;;){
break;
... // unreachable statement
}
int i=1;
if(i==1)
...
else
... // dead code
回答by JB Nizet
The compiler is able to determine that these two statement will never, ever be executed, and helps you write correct code by refusing to compile it, because this has 99.9% chance of being an error rather than a conscious choice to add statements that will never be executed.
编译器能够确定这两条语句永远不会被执行,并通过拒绝编译来帮助您编写正确的代码,因为这有 99.9% 的可能性是错误,而不是有意识地选择添加永远不会执行的语句被执行。
回答by Aniket Thakur
Unreachable code is meaningless and redundant. If you have some unreachable code in your program it is a mistake and needs to be fixed. Hence compiler throws an error.
无法访问的代码是无意义和多余的。如果您的程序中有一些无法访问的代码,那就是一个错误,需要修复。因此编译器会抛出错误。
You can refer to similar questions below
您可以参考以下类似问题
Unreachable code: error or warning?and Why does Java have an "unreachable statement" compiler error?