我试图了解下面的异常代码在运行时打印的内容。我知道它打印的是什么“adb”,但我不明白为什么要打印它。
public class MyClass {
static String str = "a";
public static void main(String[] args) {
new MyClass().method1();
System.out.println(str);
}
void method1() {
try {
method2();
}
catch (Exception e) {
str += "b";
}
}
void method2() throws Exception {
try {
method3();
str += "c";
}
catch (Exception e) {
throw new Exception();
}
finally {
str += "d";
}
method3();
str += "e";
}
void method3() throws Exception {
throw new Exception();
}
}
当调用method3()时,它抛出一个新的异常,该异常被method2()捕获,同样抛出一个新的异常,该异常被method1()捕获,在字符串中添加“b”,然后在finally块中执行method2(),添加“d”?那么为什么不是“abd”,而是“adb”呢?
UYOU
相关分类