如何捕获事件调度线程 (EDT) 异常?
我正在使用一个名为 MyExceptionHandler
的类,它实现了 Thread.UncaughtExceptionHandler
来处理项目中的正常异常。
据我了解,此类无法捕获 EDT 异常,因此我尝试在 main()
方法中使用它来处理 EDT 异常:
public static void main( final String[] args ) {
Thread.setDefaultUncaughtExceptionHandler( new MyExceptionHandler() ); // Handle normal exceptions
System.setProperty( "sun.awt.exception.handler",MyExceptionHandler.class.getName()); // Handle EDT exceptions
SwingUtilities.invokeLater(new Runnable() { // Execute some code in the EDT.
public void run() {
JFrame myFrame = new JFrame();
myFrame.setVisible( true );
}
});
}
但直到现在它都不起作用。例如,在初始化 JFrame 时,我从构造函数中的捆绑文件加载其标签,如下所示:
setTitle( bundle.getString( "MyJFrame.title" ) );
我从捆绑文件中删除了键 MyJFrame.title
以测试异常处理程序,但它不起作用!异常通常会打印在日志中。
我在这里做错了什么吗?
I am using a class called MyExceptionHandler
that implements Thread.UncaughtExceptionHandler
to handle normal exceptions in my project.
As I understand this class can't catch the EDT exceptions, so I tried to use this in the main()
method to handle EDT exceptions:
public static void main( final String[] args ) {
Thread.setDefaultUncaughtExceptionHandler( new MyExceptionHandler() ); // Handle normal exceptions
System.setProperty( "sun.awt.exception.handler",MyExceptionHandler.class.getName()); // Handle EDT exceptions
SwingUtilities.invokeLater(new Runnable() { // Execute some code in the EDT.
public void run() {
JFrame myFrame = new JFrame();
myFrame.setVisible( true );
}
});
}
But untill now it's not working. For example while initializing a JFrame I load its labels from a bundle file in the constructor like this:
setTitle( bundle.getString( "MyJFrame.title" ) );
I deleted the key MyJFrame.title
from the bundle file to test the exception handler, but it didn't work! The exception was normally printed in the log.
Am I doing something wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
EDT 异常处理程序不使用 Thread.UncaughtExceptionHandler。相反,它调用具有以下签名的方法:
将其添加到 MyExceptionHandler,它应该可以工作。
有关此内容的“文档”可在
EventDispatchThread
中找到,它是java.awt
中的包私有类。引用handleException()
的 javadoc:Sun 到底是如何期望你找到这个的,我不知道。
这是一个完整的示例,它可以捕获 EDT 内外的异常:
应该可以。
The EDT exception handler doesn't use
Thread.UncaughtExceptionHandler
. Instead, it calls a method with the following signature:Add that to
MyExceptionHandler
, and it should work.The "documentation" for this is found in
EventDispatchThread
, which is a package-private class injava.awt
. Quoting from the javadoc forhandleException()
there:How exactly Sun expected you find this, I have no idea.
Here's a complete example which catches exceptions both on and off the EDT:
That should do it.
总结以上内容...使用较新的 Java,您可以这样做:
Summarizing the above... with newer Java you can just do this:
仅提供一些额外信息,在许多情况下,即使在 1.5 和 1.6 中,Throwables 也可能被 EDT 的 UncaughtExceptionHandler 捕获。查看1.5.0_22中EventDispatchThread的源代码:
根据这段代码,只有3种方式Throwable不会被EDT线程的UncaughtExceptionHandler捕获:
Just for some extra info, in many cases Throwables can get caught by the EDT's UncaughtExceptionHandler even in 1.5 and 1.6. Looking at the source code for EventDispatchThread in 1.5.0_22:
According to this code, there are only 3 ways a Throwable will not be caught by the EDT Thread's UncaughtExceptionHandler: