考虑以下代码:当main抛出异常时会发生什么?
public class Test {
public static void main(String[] args) throws Exception {
mayFail();
System.out.println("This shouldn't run");
}
public static void mayFail() throws Exception{
throw new Exception();
}
}
我知道什么时候该Exception
是throw
“N main
将终止。
我想知道的是有没有在JVM
(或任何地方)过程的一些国王执行时发生这种情况?
编辑:
阅读阿尔马斯后谢赫回答我设法处理这Exception
像这样:如果我理解它正确,这是因为远是不言而喻
public class Test{
public static void main(String[] args) throws Exception {
UncaughtExceptionHandler a = new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
System.out.println("Uncaught Exception");
}
};
Thread.setDefaultUncaughtExceptionHandler(a);
mayFail();
System.out.println("This shouldn't run");
}
public static void mayFail() throws Exception{
throw new Exception();
}
}
所以呢?如果没有UncaughtExceptionHandler
该程序只是终止而不做任何事情?
@Charlie你没明白我的问题。 – 2014-12-13 15:41:39
这就是为什么我删除了我的评论:I – Charlie 2014-12-13 15:41:57