情形一

执行下面的代码会发现finally中的​​"执行了finally。。。"​​并不会被打印。

public class Demo {

public static void main(String[] args) {

try {
System.exit(0);
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("执行了finally。。。");
}
}

}

情形二

另外一种情况就是没有执行到try就返回也会导致finally不会执行

public class Demo {

public static void main(String[] args) {

if (true) return;// 直接返回,没有执行try就返回了方法
try {
System.out.println("执行try代码块");
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("执行了finally。。。");
}
}

}

宕机咱就不考虑了,这种情况是肯定不会执行的

实际上这两种情形一般人是肯定不会这样写的,只是想说明一下很多问题并不能太绝对。

我相信也有这样的面试题,遇到这种题目只能认倒霉。