Bra*_*rad 20 java exception try-catch event-dispatch-thread
我正在使用一个名为MyExceptionHandlerimplements 的类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 );
}
});
}
Run Code Online (Sandbox Code Playgroud)
但直到现在它还没有用.例如,在初始化JFrame时,我从构造函数中的bundle文件加载其标签,如下所示:
setTitle( bundle.getString( "MyJFrame.title" ) );
Run Code Online (Sandbox Code Playgroud)
我MyJFrame.title从bundle文件中删除了密钥以测试异常处理程序,但它不起作用!例外通常打印在日志中.
我在这里做错了吗?
uck*_*man 30
EDT异常处理程序不使用Thread.UncaughtExceptionHandler.相反,它调用具有以下签名的方法:
public void handle(Throwable thrown);
Run Code Online (Sandbox Code Playgroud)
添加它MyExceptionHandler,它应该工作.
这里有"文档" EventDispatchThread,它是一个包私有类java.awt.从handleException()那里引用javadoc :
/**
* Handles an exception thrown in the event-dispatch thread.
*
* <p> If the system property "sun.awt.exception.handler" is defined, then
* when this method is invoked it will attempt to do the following:
*
* <ol>
* <li> Load the class named by the value of that property, using the
* current thread's context class loader,
* <li> Instantiate that class using its zero-argument constructor,
* <li> Find the resulting handler object's <tt>public void handle</tt>
* method, which should take a single argument of type
* <tt>Throwable</tt>, and
* <li> Invoke the handler's <tt>handle</tt> method, passing it the
* <tt>thrown</tt> argument that was passed to this method.
* </ol>
*
* If any of the first three steps fail then this method will return
* <tt>false</tt> and all following invocations of this method will return
* <tt>false</tt> immediately. An exception thrown by the handler object's
* <tt>handle</tt> will be caught, and will cause this method to return
* <tt>false</tt>. If the handler's <tt>handle</tt> method is successfully
* invoked, then this method will return <tt>true</tt>. This method will
* never throw any sort of exception.
*
* <p> <i>Note:</i> This method is a temporary hack to work around the
* absence of a real API that provides the ability to replace the
* event-dispatch thread. The magic "sun.awt.exception.handler" property
* <i>will be removed</i> in a future release.
*/
Run Code Online (Sandbox Code Playgroud)
太阳如何期待你发现这一点,我不知道.
这是一个完整的例子,可以捕获EDT内外的异常:
import javax.swing.SwingUtilities;
public class Test {
public static class ExceptionHandler
implements Thread.UncaughtExceptionHandler {
public void handle(Throwable thrown) {
// for EDT exceptions
handleException(Thread.currentThread().getName(), thrown);
}
public void uncaughtException(Thread thread, Throwable thrown) {
// for other uncaught exceptions
handleException(thread.getName(), thrown);
}
protected void handleException(String tname, Throwable thrown) {
System.err.println("Exception on " + tname);
thrown.printStackTrace();
}
}
public static void main(String[] args) {
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler());
System.setProperty("sun.awt.exception.handler",
ExceptionHandler.class.getName());
// cause an exception on the EDT
SwingUtilities.invokeLater(new Runnable() {
public void run() {
((Object) null).toString();
}
});
// cause an exception off the EDT
((Object) null).toString();
}
}
Run Code Online (Sandbox Code Playgroud)
应该这样做.