【发布时间】:2016-12-22 21:07:17
【问题描述】:
我在准备 OCA 时遇到了一个练习,我不明白为什么程序会打印:abce 3 而不是 abcde 3。这里的程序:
'public static void main(String[] args) {
System.out.print("a");
try{
System.out.print("b");
throw new IllegalArgumentException();
}catch(IllegalArgumentException e){
System.out.print("c");
throw new RuntimeException("1");
}catch(RuntimeException e) {
System.out.print("d");
throw new RuntimeException("2");
}finally {
System.out.print("e");
throw new RuntimeException("3");
}
}'
任何解释为什么它忽略最后一个 catch 块将非常感激!
【问题讨论】:
-
Another problem is that the exception thrown in the last catch block is simply ignored or masked by the one thrown in the finally block。是的,这是一个问题,但它是 Java 内置的一个问题 :) 不要从 finally 块中抛出异常。