小例子:
public class App {
public static void main(String[] args) {
throw new RuntimeException("Test exception");
}
}
按预期打印以下内容:
Exception in thread "main" java.lang.RuntimeException: Test exception
at App.main(App.java:5)
让我们修改这个程序:
public class App {
public static void main(String[] args) {
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
if (e instanceof RuntimeException) throw (RuntimeException)e;
}
});
throw new RuntimeException("Test exception");
}
}
印刷:
Exception: java.lang.RuntimeException thrown from the UncaughtExceptionHandler in thread "main"
问题:
为什么消息不包含有关其发生位置的任何信息,并且没有堆栈跟踪消息?
至于我,异常消息看起来很奇怪。