我试图弄清楚 in 的执行try-catch-finally
顺序java
。我认为执行顺序应该是
- 尝试
- 捕获(如果发生错误/捕获异常)
- finally (是否捕获到异常)
但我对以下结果感到困惑
public class TryCatchFinally {
static int i = 0;
public static void main(String[] args) {
try {
System.out.println(i++);
main(args);
} catch (StackOverflowError e) {
System.out.println("Catch");
} finally {
System.out.println("Finally");
}
}
}
输出(输出的一部分)
9127
9128
9129
9130
CatcFCatch // what is the wrong here???
Finally
Finally // there are more Finally printed here.
我的问题是这里到底发生了什么?
让我添加更多为什么它不打印"Catch"
???
我在运行时将其输出IntelliJ IDEA
。但是当我跑进去时,我会terminal
按如下方式退出。
9151
9152
9153
9154CatchFinallyCatch
Finally
Finally
Finally
Finally