Finally不一定会执行的两种情况
代码如下:
/**
* 展示两种不会执行finally的情况
*
* @author Test
*
*/
public class Main {
public static void testFimally1() {
int i = 5 / 0;
try {
System.out.println("try block");
} catch (Exception e) {
System.out.println("catch block");
} finally {
System.out.println("finally block");
}
}
public static void testFimally2() {
try {
System.out.println("try block");
System.exit(0);
} catch (Exception e) {
System.out.println("catch block");
} finally {
System.out.println("finally block");
}
}
public static void main(String[] args) {
// testFimally1();
testFimally2();
}
}