public class ExceptionTest {
public void method () {
try {
System.out.println("try");
System.exit(0);
return;
} catch (Exception e) {
System.out.println("异常发生了!");
} finally {
System.out.println("异常处理后续的代码");
}
}
public static void main(String[] args) {
ExceptionTest test = new ExceptionTest();
test.method();
}
}
输出结果为:
分析:
System.exit方法会导致当前运行线程停止并使其它线程都终止,因此上面这段代码中的finally和catch代码块都不会被执行。
System.exit(0)表示正常退出JVM,而System.exit(1)表示异常退出JVM。参数只是通知操作系统程序的退出状态,0为正常,非0为异常。
调用 System.exit(n) 实际上等效于调用:
Runtime.getRuntime().exit(n)