交换机是否在不停地执行所有情况?
Is a switch executing all the cases without stopping?
我在 Java 8v60。我试图在 catch 块中嵌入一个关于异常组的开关。显然,案例已被识别,但一旦他们进入开关,他们就会继续检查所有可能的案例。这是 Java 错误吗?
看起来像这样:
try {
...
} catch (DateTimeParseException exc) {
...
} catch (myException exc) {
switch (exc.getEvent()) {
case EVENT_ONE :
//once EVENT_ONE gets here;
case EVENT_TWO : case EVENT_THREE :
//it keeps going everywhere;
case EVENT_FOUR :
//and so on;
default :
//and here of course too.
//but if it's not one of the above, it just appears here only
}
...
很奇怪,不是吗。有什么想法吗?
没有。这不是错误。您没有正确实施开关。它失败了。您需要在每个案例后有 break
。
例如:
switch (exc.getEvent()) {
case EVENT_ONE :
//once EVENT_ONE gets here;
break;
case EVENT_TWO : case EVENT_THREE :
//it keeps going everywhere;
break;
case EVENT_FOUR :
//and so on;
break;
这里是 official doc
相同的
Another point of interest is the break statement. Each break statement terminates the enclosing switch statement. Control flow continues with the first statement following the switch block. The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered.
switch 语句跳转到正确的值,并继续直到其他情况结束。
如果您想退出 switch 语句,则必须使用 break(或 return 在某些情况下)。
这对于处理可以以相同方式处理许多值的情况很有用:
switch (x) {
case 0:
case 1:
case 2:
System.out.println("X is smaller than 3");
break;
case 3:
System.out.println("X is 3");
case 4:
System.out.println("X is 3 or 4");
break;
}
如果案例选择也是方法的最终条件,您可以从中return。
public String checkX(int x) {
switch (x) {
case 0:
case 1:
case 2:
return "X is smaller than 3";
case 3:
return "X is 3";
case 4:
return ("X is necessary 4");
default:
return null;
}
}
}
这不是 java 错误。这是你的逻辑错误。
在每个 case
语句之后放置 break
语句以避免 fall through
情况。
我在 Java 8v60。我试图在 catch 块中嵌入一个关于异常组的开关。显然,案例已被识别,但一旦他们进入开关,他们就会继续检查所有可能的案例。这是 Java 错误吗?
看起来像这样:
try {
...
} catch (DateTimeParseException exc) {
...
} catch (myException exc) {
switch (exc.getEvent()) {
case EVENT_ONE :
//once EVENT_ONE gets here;
case EVENT_TWO : case EVENT_THREE :
//it keeps going everywhere;
case EVENT_FOUR :
//and so on;
default :
//and here of course too.
//but if it's not one of the above, it just appears here only
}
...
很奇怪,不是吗。有什么想法吗?
没有。这不是错误。您没有正确实施开关。它失败了。您需要在每个案例后有 break
。
例如:
switch (exc.getEvent()) {
case EVENT_ONE :
//once EVENT_ONE gets here;
break;
case EVENT_TWO : case EVENT_THREE :
//it keeps going everywhere;
break;
case EVENT_FOUR :
//and so on;
break;
这里是 official doc
相同的
Another point of interest is the break statement. Each break statement terminates the enclosing switch statement. Control flow continues with the first statement following the switch block. The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered.
switch 语句跳转到正确的值,并继续直到其他情况结束。
如果您想退出 switch 语句,则必须使用 break(或 return 在某些情况下)。
这对于处理可以以相同方式处理许多值的情况很有用:
switch (x) {
case 0:
case 1:
case 2:
System.out.println("X is smaller than 3");
break;
case 3:
System.out.println("X is 3");
case 4:
System.out.println("X is 3 or 4");
break;
}
如果案例选择也是方法的最终条件,您可以从中return。
public String checkX(int x) {
switch (x) {
case 0:
case 1:
case 2:
return "X is smaller than 3";
case 3:
return "X is 3";
case 4:
return ("X is necessary 4");
default:
return null;
}
}
}
这不是 java 错误。这是你的逻辑错误。
在每个 case
语句之后放置 break
语句以避免 fall through
情况。