Java 带有中断的无法访问的语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20157175/
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 with Break
提问by KyleCrowley
So I had a previous question but realized I posted the wrong offending code. I've marked the offending statements below.
所以我有一个以前的问题,但意识到我发布了错误的违规代码。我已经标记了下面的违规言论。
What I am trying to do is set the precedence for each of the operators with that switch statement.
我想要做的是使用该 switch 语句为每个运算符设置优先级。
Maybe someone could point me in the right direction.
也许有人可以指出我正确的方向。
Just as a note, I AM running JAVA 7 so String Switch will work.
请注意,我正在运行 JAVA 7,因此 String Switch 将起作用。
Code
代码
opType.java
操作类型
import java.io.*;
public final class opType {
public static opType ADD = new opType( "Add" );
public static opType SUB = new opType( "Sub" );
public static opType MULT = new opType( "Mult" );
public static opType DIV = new opType( "Div" );
public static opType MOD = new opType( "Mod" );
public static opType LPAR = new opType( "LParen" );
public static opType RPAR = new opType( "RParen" );
protected String name;
private opType( String n )
{
name = n;
}
public String getName()
{
return name;
}
Operator.java
运算符.java
public class Operator extends Token {
protected opType val;
public boolean isOperator() { return true; }
public boolean isOperand() { return false; }
protected int getPrec()
{
switch(val.getName())
{
case "LParen":
{
return 0;
break; //unreachable
}
case "RParen":
{
return 0;
break; //unreachable
}
case "Mult":
{
return 1;
break; //unreachable
}
case "Div":
{
return 1;
break; //unreachable
}
case "Mod":
{
return 1;
break; //unreachable
}
case "Add":
{
return 2;
break; //unreachable
}
case "Sub":
{
return 2;
break; //unreachable
}
}
return 0;
}
public static int compare( Operator a, Operator b )
{
if( a.getPrec() == b.getPrec() )
return 0;
else if( a.getPrec() < b.getPrec() )
return -1;
else
return 1;
}
public opType getVal() { return val; }
public Operator( opType v ) { val = v; }
}
采纳答案by Blub
If you put a return
, then the function returns before the break
is executed and therefore the break
will never be reached.
如果您放置 a return
,则该函数在break
执行之前返回,因此break
永远不会到达。
Instead you could use a variable that you set to a desired value and after the switch return that. Or just get rid of the break
statements.
相反,您可以使用您设置为所需值的变量,并在开关返回之后。或者只是摆脱这些break
陈述。
回答by Jigar Joshi
you already have return
which will make the break
unreachable
你已经拥有return
这将使break
无法到达的
回答by Paul Richter
The return
statement effectively exits the method immediately. Since you've placed return statements inside the switch
block for each case
, whichever case
is matched will, according to your code, return whatever value is indicated immediately. The break
therefore cannot be executed, hence the error. You have two options:
该return
语句有效地立即退出该方法。既然你已经放置return语句里面switch
的每个块case
,无论case
是匹配的意愿,根据你的代码,任何值当即表示回报。的break
因此不能被执行,因此错误。您有两个选择:
1- Set a value, and return at the end of the method:
1- 设置一个值,并在方法结束时返回:
protected int getPrec(){
int prec = 0;
switch(val.getName()) {
case "LParen":
prec = 0;
break;
case "RParen":
prec = 0;
break;
case "Mult":
prec = 1;
break;
case "Div":
prec = 1;
break;
case "Mod":
prec = 1;
break;
case "Add":
prec = 2;
break;
case "Sub":
prec = 2;
break;
default:
prec = 0;
break; // technically unnecessary since we're at the end already but adding for completeness.
}
return prec;
}
2- Ditch the break;
statements and keep the return
statements as you've written them.
2- 抛弃break;
陈述,保留return
你写下的陈述。
Personally I would prefer the first option as its cleaner and more readable to me. Plus it makes it easier to expand whatever actions need to be done in one or more cases if need be in the future.
就我个人而言,我更喜欢第一个选项,因为它对我来说更清晰、更易读。此外,如果将来需要,它可以更轻松地扩展在一个或多个案例中需要执行的任何操作。
By the way, watch your naming convention. You presently have:
顺便说一下,注意你的命名约定。您目前拥有:
public final class opType // bad naming
Since this is a class, the Java standard is to capitalize the first letter of the class. So it should be:
由于这是一个类,Java 标准是将类的第一个字母大写。所以应该是:
public final class OpType // good naming
回答by Makoto
The reason that the code is unreachable is due to the return
behaving like a break
in that context - they both complete abruptly.
代码无法访问的原因是由于在该上下文中的return
行为类似于 a break
- 它们都突然完成。
If a statement completes abruptly, then execution at that line is immediately returned to its appropriate context; if it's a break
, it'll attempt to either exit the switch
or return to its associated label if one exists; if it's a return
, it will return to its caller, with or without a value.
如果一个语句突然完成,那么在该行的执行会立即返回到其适当的上下文;如果是 a break
,它将尝试退出switch
或返回到其关联标签(如果存在);如果它是 a return
,它将返回给它的调用者,有或没有值。
This is why the code is unreachable: the line of code after the return
can notbe reached.
这就是为什么代码无法访问: 代码后,该行return
无法到达。
To really understand what that means or entails, we have to look at the Java Language Specification, specifically 14.1:
要真正理解这意味着什么或意味着什么,我们必须查看 Java 语言规范,特别是14.1:
Every statement has a normal mode of execution in which certain computational steps are carried out. The following sections describe the normal mode of execution for each kind of statement.
If all the steps are carried out as described, with no indication of abrupt completion, the statement is said to complete normally. However, certain events may prevent a statement from completing normally:
The break (§14.15), continue (§14.16), and return (§14.17) statements cause a transfer of control that may prevent normal completion of statements that contain them.
Evaluation of certain expressions may throw exceptions from the Java Virtual Machine (§15.6). An explicit throw (§14.18) statement also results in an exception. An exception causes a transfer of control that may prevent normal completion of statements.
If such an event occurs, then execution of one or more statements may be terminated before all steps of their normal mode of execution have completed; such statements are said to complete abruptly.
An abrupt completion always has an associated reason, which is one of the following:
A
break
with no labelA
break
with a given labelA
continue
with no labelA
continue
with a given labelA
return
with no valueA
return
with a given valueA
throw
with a given value, including exceptions thrown by the Java Virtual MachineThe terms "complete normally" and "complete abruptly" also apply to the evaluation of expressions (§15.6). The only reason an expression can complete abruptly is that an exception is thrown, because of either a throw with a given value (§14.18) or a run-time exception or error (§11, §15.6).
If a statement evaluates an expression, abrupt completion of the expression always causes the immediate abrupt completion of the statement, with the same reason. All succeeding steps in the normal mode of execution are not performed.
Unless otherwise specified in this chapter, abrupt completion of a substatement causes the immediate abrupt completion of the statement itself, with the same reason, and all succeeding steps in the normal mode of execution of the statement are not performed.
Unless otherwise specified, a statement completes normally if all expressions it evaluates and all substatements it executes complete normally.
每个语句都有一个正常的执行模式,其中执行某些计算步骤。以下部分描述了每种语句的正常执行模式。
如果所有步骤都按照描述执行,没有突然完成的迹象,则称该语句正常完成。但是,某些事件可能会阻止语句正常完成:
break(第 14.15 节)、continue(第 14.16 节)和 return(第 14.17 节)语句会导致控制转移,这可能会阻止包含它们的语句的正常完成。
某些表达式的计算可能会从 Java 虚拟机(第 15.6 节)中抛出异常。显式 throw (§14.18) 语句也会导致异常。异常会导致控制转移,这可能会阻止语句的正常完成。
如果发生此类事件,则一个或多个语句的执行可能会在其正常执行模式的所有步骤完成之前终止;据说这样的陈述突然完成。
突然完成总是有一个相关的原因,它是以下之一:
A
break
没有标签
break
具有给定标签的AA
continue
没有标签
continue
具有给定标签的AA
return
没有价值
return
具有给定值的A
throw
具有给定值的A ,包括 Java 虚拟机抛出的异常术语“正常完成”和“突然完成”也适用于表达式的计算(第 15.6 节)。表达式可以突然完成的唯一原因是抛出异常,因为抛出具有给定值(第 14.18 节)或运行时异常或错误(第 11 节、第 15.6 节)。
如果语句对表达式求值,则表达式的突然完成总是会导致语句的立即突然完成,原因相同。正常执行模式下的所有后续步骤都不会执行。
除非本章另有规定,子语句的突然完成会导致语句本身立即突然完成,原因相同,并且不会执行语句正常执行模式下的所有后续步骤。
除非另有说明,否则如果它评估的所有表达式和它执行的所有子语句正常完成,则语句正常完成。