Nic*_*net 18 java swing exception-handling awt
我们想在我们的应用程序日志中跟踪这些异常 - 默认情况下,Java只是将它们输出到控制台.
ToY*_*nos 12
从Java 7开始,你必须采用不同的方式,因为sun.awt.exception.handlerhack不再起作用了.
这是解决方案(来自Java 7中的Uncaught AWT Exceptions).
// 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());
}
});
Run Code Online (Sandbox Code Playgroud)
she*_*non 10
美国东部时间和美国东部时间之外的未捕获例外情况有所区别.
另一个问题是两者都有解决方案,但如果你只想让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())
}
}
Run Code Online (Sandbox Code Playgroud)