gpt4 book ai didi

java - 如何配置 WildFly 来打印运行时异常?

转载 作者:太空宇宙 更新时间:2023-11-04 10:48:42 24 4
gpt4 key购买 nike

该环境是一个部署在 WildFly 11.0.0.Final 中的 Java 程序,以及一个启动 standalone.sh 并保持打开状态并显示输出的终端窗口。

显式输出,例如 PrintStream#printlnThrowable#printStackTrace按预期打印,但当出现运行时异常时,例如 ArrayIndexOutOfBoundsException被抛出,自然不会被捕获,因此不会显式打印,不会打印任何内容。现在我被迫捕获运行时异常并打印它们,这显然很麻烦。

有没有一种方法可以让 WildFly 自动打印运行时异常的堆栈跟踪,就像人们期望的那样?

最佳答案

使用 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();

关于java - 如何配置 WildFly 来打印运行时异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48089763/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com