【发布时间】:2015-06-10 17:21:51
【问题描述】:
public class TestException extends except2 {
public static void main(String[] args)throws Exception {
try {
try {
throw new TestException();
}
catch (final TestException e){
}
throw new except2();
}
catch (TestException a){
}
catch (Exception e){
throw e;
}
}
public TestException(){
}
}
class except2 extends Exception{
}
大家好,
我的 JDK 版本是 8u45,现在是最新版本。
我想知道在 SE 8 中仍然可以使用“带有最终异常的精确重新抛出”吗?
作为代码,如果我将“抛出异常”关闭,它将是编译错误,但根据SE7的“精确重新抛出最终异常”功能应该可以忽略它。
另外一个问题是我们都知道如果嵌套的try box发生了异常,我们还是应该把它扔到外层的catch box来避免编译错误,我本来以为只要抛出任意类型的异常就可以了& 它会做,我的测试结果也是如此,我认为这是让编译器知道 try box 和 catch box 中存在异常也得到了它。
但是如果我像下面的代码一样改变它:
public class TestException extends except2 {
public static void main(String[] args)throws Exception {
try {
try {
throw new ArithmeticException();
} catch (final TestException e){
throw e;
}
} catch (TestException a){
} catch (Exception e){
throw e;
}
}
}
(final TestException e) 部分将是编译错误消息:
"com.xxx.TestException" 异常不会在相应的 try 块中抛出",
我很困惑,因为如果嵌套的 catch 块无法处理异常,它应该转到外部。
那么,如果我在外部 try 块的末尾抛出一个 ArithmeticException,如下所示:
try {
try {
throw new TestException();
}
catch (final TestException e){
System.out.println("d");
}
throw new ArithmeticException();
}
catch (TestException a){
}
catch (Exception e){
throw e;
}
与外部捕获框catch (TestException a){}相同的错误
为什么会这样?
它应该被(Exception e) 块捕获。
如果我不能从嵌套try块的第一个异常中抛出不同类型的异常,为什么我可以在第一段代码中抛出except2?
【问题讨论】:
-
你能压缩你的代码吗?有很多空白。
-
@SotiriosDelimanolis thx 伙计,新来这里的人,抱歉