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

有帮助吗?

解决方案

EDT 内和 EDT 外的未捕获异常之间存在区别。

另一个问题有两个解决方案 但如果你只想把 EDT 部分嚼碎......

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())
  }
}

其他提示

从 Java 7 开始,您必须以不同的方式进行操作 sun.awt.exception.handler hack 不再起作用了。

这是解决方案 (从 Java 7 中未捕获的 AWT 异常).

// 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());
    }
});

一点补充 谢姆农答案:
第一次在 EDT 中发生未捕获的 RuntimeException(或错误)时,它会查找属性“sun.awt.exception.handler”并尝试加载与该属性关联的类。EDT 需要 Handler 类有一个默认构造函数,否则 EDT 将不会使用它。
如果您需要在处理故事中引入更多动态,则必须使用静态操作来执行此操作,因为该类是由 EDT 实例化的,因此没有机会访问除静态之外的其他资源。下面是我们正在使用的 Swing 框架中的异常处理程序代码。它是为 Java 1.4 编写的,并且运行得很好:

public class AwtExceptionHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(AwtExceptionHandler.class);

    private static List exceptionHandlerList = new LinkedList();

    /**
     * WARNING: Don't change the signature of this method!
     */
    public void handle(Throwable throwable) {
        if (exceptionHandlerList.isEmpty()) {
            LOGGER.error("Uncatched Throwable detected", throwable);
        } else {
            delegate(new ExceptionEvent(throwable));
        }
    }

    private void delegate(ExceptionEvent event) {
        for (Iterator handlerIterator = exceptionHandlerList.iterator(); handlerIterator.hasNext();) {
            IExceptionHandler handler = (IExceptionHandler) handlerIterator.next();

            try {
                handler.handleException(event);
                if (event.isConsumed()) {
                    break;
                }
            } catch (Throwable e) {
                LOGGER.error("Error while running exception handler: " + handler, e);
            }
        }
    }

    public static void addErrorHandler(IExceptionHandler exceptionHandler) {
        exceptionHandlerList.add(exceptionHandler);
    }

    public static void removeErrorHandler(IExceptionHandler exceptionHandler) {
        exceptionHandlerList.remove(exceptionHandler);
    }

}

希望能帮助到你。

有两种方法:

  1. /* 在 EDT 上安装 Thread.UncaughtExceptionHandler */
  2. 设置系统属性:System.setProperty("sun.awt.Exception.handler",MyExceptionHandler.class.getName());

我不知道后者是否适用于非 SUN jvm。

--

事实上,第一个是不正确的,它只是一种检测崩溃线程的机制。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top