在AWT队列线程调试异常 [英] Debug exceptions in AWT queue thread

查看:189
本文介绍了在AWT队列线程调试异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个组件进行自定义绘制Swing应用程序。当我做一些错误的画code和抛出一个异常,这种情况是难以调试。取而代之的调试器被抓,一个弹出窗口显示的异常信息。此外,<一个href=\"http://stackoverflow.com/questions/3020757/java-does-the-edt-restart-or-not-when-an-exception-is-thrown\">the螺纹似乎被重新启动和作为异常是编码误差的结果,它是一次又一次地示出

I am developing a Swing application with a component performing custom painting. When I make some mistake in the painting code and an exception is thrown, the situation is hard to debug. Instead of being caught by the debugger, a popup shows with the exception information. Moreover, the thread seems to be restarted, and as the exception is a result of coding error, it is shown again and again.

当我幸运地切换到调试器(这是困难的,因为越来越多的弹出窗口滚滚而来的应用程序获取漆请求),调试控制台显示我像一个异常信息:

When I am lucky enough to switch into the debugger (which is difficult, because more and more popups keep coming as the application gets paint requests), the debugging console shows me an exception information like:

重度:未捕获的异常在线程抛出[AWT-EventQueue的-0,6,主]

SEVERE: Uncaught exception thrown in Thread[AWT-EventQueue-0,6,main]

....栈如下

我的应用程序是用Scala和我使用的IntelliJ IDEA 14.我未捕获的主线程异常处理调试器精未捕获的异常 C $ C>在 Java异常断点)使任何异常断点,但在AWT线程中的异常都没有。

My application is written in Scala and I am using IntelliJ IDEA 14. My uncaught main thread exceptions are handled fine by the debugger (I have Uncaught exception enabled for Any exception breakpoint enabled in the Java Exception Breakpoints), but exceptions in AWT threads are not.

我试图安装一个处理程序,这如何可以检测异常时一直在全球范围内的Java描述抛出?回答,但我的处理程序似乎并没有被触发。

I have tried installing a handler as described in this How can I detect when an Exception's been thrown globally in Java? answer, but my handler does not seem to be triggered.

我想实现如下(按重要性排序):

I would like to achieve following (in order of importance):


  1. 避免AWT线程上重新启动异常,或者至少prevent弹出展示

  2. 处理,而不是被印在控制台在调试器中捕获的异常

(注意:虽然这是Scala的应用程序,我认为结果会是Java的相同,因此Java标记)

(Note: while this is Scala application, I assume the behaviour would be the same for Java, hence the Java tag).

推荐答案

根据这个链接,你有同时处理普通的例外 EDT异常不使用旧 sun.awt.exception。处理器破解

According to this link, you have to handle both regular Exception and EDT Exception without using the old sun.awt.exception.handler hack (which does not work anymore since Java 7)

下面是你的的ExceptionHandler

public static class ExceptionHandler implements Thread.UncaughtExceptionHandler
{
    public void uncaughtException(Thread thread, Throwable thrown)
    {
        // TODO handle your Exception here
    }
}

用法:

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

这篇关于在AWT队列线程调试异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆