首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Java中捕获AWT线程异常?

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

Stack Overflow用户
提问于 2008-09-18 19:04:00
回答 2查看 6.7K关注 0票数 18

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2008-09-18 21:27:41

EDT中的未捕获异常和EDT外部的未捕获异常之间存在差异。

Another question has a solution for both,但是如果你只想要美国东部夏令时的那部分...

class AWTExceptionHandler {

  public void handle(Throwable t) {
    try {
      // insert your exception handling code here
      // or do nothing to make it go away
    } catch (Throwable t) {
      // don't let the exception get thrown out, will cause infinite looping!
    }
  }

  public static void registerExceptionHandler() {
    System.setProperty('sun.awt.exception.handler', AWTExceptionHandler.class.getName())
  }
}
票数 10
EN

Stack Overflow用户

发布于 2015-01-09 18:57:04

从Java7开始,你必须以不同的方式来做,因为sun.awt.exception.handler黑客不再工作了。

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());
    }
});
票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/95767

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档