在 Java-8 中捕获多个异常
在尝试我在方法中发现的多捕获功能时,一切都按预期工作正常。m1()
但是,在相同的代码中不编译。我刚刚更改了语法以减少代码行数。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()