gpt4 book ai didi

java - 如何在 Java 中捕获 AWT 线程异常?

转载 作者:太空狗 更新时间:2023-10-29 22:33:46 26 4
gpt4 key购买 nike

我们希望在我们的应用程序日志中跟踪这些异常 - 默认情况下,Java 只是将它们输出到控制台。

最佳答案

从 Java 7 开始,您必须以不同的方式执行此操作,因为 sun.awt.exception.handler hack 不再有效。

Here is the solution (来自 Uncaught AWT Exceptions in Java 7 )。

// Regular Exception
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler());

// EDT Exception
SwingUtilities.invokeAndWait(new Runnable()
{
public void run()
{
// We are in the event dispatching thread
Thread.currentThread().setUncaughtExceptionHandler(new ExceptionHandler());
}
});

关于java - 如何在 Java 中捕获 AWT 线程异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/95767/

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