环境是部署在WildFly 11.0.0.Final中的Java程序,以及一个启动standalone.sh
并保持打开状态显示输出的终端窗口。
显式输出(例如PrintStream#println
和Throwable#printStackTrace
)按预期打印,但是当抛出运行时异常(例如ArrayIndexOutOfBoundsException
)时,自然未捕获并因此未明确打印,则不会打印任何内容。现在我被迫捕获运行时异常并打印它们,这显然很麻烦。
有没有办法让WildFly自动打印运行时异常的堆栈跟踪,就像人们期望的那样?
答案 0 :(得分:0)
使用Thread.UncaughtExceptionHandler
Thread thread = new Thread() {
public void run() {
System.out.println("Thread start");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Throw exception");
throw new RuntimeException();
}
};
Thread.UncaughtExceptionHandler handler = new Thread.UncaughtExceptionHandler() {
public void uncaughtException(Thread t, Throwable e) {
System.out.println("Uncaught thread " + t + " exception: " + e);
}
};
thread.setUncaughtExceptionHandler(handler);
thread.start();