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

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

问题描述

我正在开发具有执行自定义绘画的组件的Swing应用程序。当我在绘画代码中犯了一些错误,抛出异常时,情况很难调试。而不是被调试器捕获,弹出窗口显示异常信息。此外,线程似乎重新启动,并且由于编码错误的结果是异常的,它会一再显示。

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:


SEVERE:Thread中引发了未捕获的异常[AWT-EventQueue-0,6,main]

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

....堆栈跟随

我的应用程序是用Scala编写的,我正在使用IntelliJ IDEA 14.我的未捕获的主线程异常被调试器处理正常(我有未捕获异常启用 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线程重新启动异常,或至少阻止弹出窗口显示

  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.handler hack(从Java 7起不再工作)

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天全站免登陆