【发布时间】:2020-04-26 23:32:49
【问题描述】:
在尝试我在 m1() 方法中发现的 multi-catch 功能时,一切正常。
但是,在m2() 中,相同的代码无法编译。我刚刚更改了语法以减少代码行数。
public class Main {
public int m1(boolean bool) {
try {
if (bool) {
throw new Excep1();
}
throw new Excep2();
//This m1() is compiling abs fine.
} catch (Excep1 | Excep2 e) {
return 0;
}
}
public int m2(boolean b) {
try {
throw b ? new Excep1() : new Excep2();
//This one is not compiling.
} catch (Excep1 | Excep2 e) {
return 0;
}
}
private static interface I {
}
private static class Excep1 extends Exception implements I {
}
private static class Excep2 extends Exception implements I {
}
}
为什么方法 m2() 不编译?
【问题讨论】:
-
你遇到了什么编译错误?
标签: java exception java-8 try-catch