try-catch- finally块中, finally块唯一不执行的情况是什么?
除非在try块或者catch块中调用了退出虚拟机的方法(即System.exit(1);),否则不管在try块、catch块中执行怎样的代码,出现怎样的情况,异常处理的finally块总是会被执行的
public class TryTest{
public static void main(String[] args){
test();
}
public static void test(){
try{
System.out.println("try");
int i = 1 / 0;
System.exit(1);
}catch(Exception e){
e.printStackTrace();
System.exit(1);
}finally{
System.out.println("finally");
}
}
}
输出结果:
try java.lang.ArithmeticException: / by zero at com.tangyang.sometest.TEST.test(TEST.java:11) at com.tangyang.sometest.TEST.main(TEST.java:5)
使用System.exit(1)语句可以退出Java虚拟机,因此不执行finally

浙公网安备 33010602011771号