问题描述
我知道当异常发生时,catch 块会在捕获异常后捕获那些异常,然后进入 finally 块。 当我运行像
这样的代码时 public static void main(String... args) {
try {
throw new ClassCastException();
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException();
} catch (RuntimeException e) {
System.out.println("This catch block must executed");
} finally {
throw new ArithmeticException();
}
}
输出:
这个catch块必须执行
线程“main”中的异常 java.lang.ArithmeticException
在 com.oop.design.demo.Main.main(Main.java:31)
output is expected ,上面的执行顺序我很清楚。但是每当代码更改为
public static void main(String... args) {
try {
throw new ClassCastException();
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException();
} catch (RuntimeException e) {
throw new IOException();
} finally {
throw new ArithmeticException();
}
}
输出:
线程“main”中的异常 java.lang.ArithmeticException
在 com.oop.design.demo.Main.main(Main.java:31)
我的问题是为什么不执行 catch 块?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)