Java (SWT/JFace) - 处理 Runnable 内的所有异常
我有一个 SWT/JFace 应用程序,它使用 Realm(不确定概念)类将主程序作为线程运行。我试图在我的主代码周围使用 try/catch 块来捕获任何未捕获的异常:
public static void main(String args[]) {
// ref: http://forums.instantiations.com/viewtopic.php?f=1&t=1583
Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
public void run() {
try {
PropertyConfigurator.configure("log4j.properties");
MainWindow window = new MainWindow();
window.setBlockOnOpen(true);
window.open();
Display.getCurrent().dispose();
} catch (Exception e) {
MessageDialog.openError(null, "Error", "Error occurred: " + e.getMessage());
logger.error("Error!!!", e);
e.printStackTrace();
}
}
});
}
错误会被很好地抛出回 window.open()
行,但随后会传递到 Realm,所以永远不会到达catch
块。这是堆栈跟踪的结尾:
at org.eclipse.jface.window.Window.runEventLoop(Window.java:825)
at org.eclipse.jface.window.Window.open(Window.java:801)
at com.ism.MainWindow$1.run(MainWindow.java:210) <-- "window.open();"
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at com.ism.MainWindow.main(MainWindow.java:204) <-- "Realm.runWithDefault....."
尝试在 Realm.runWithDefault 周围放置 try/catch,但这也不起作用。
在这种情况下如何捕获所有异常?
I have an SWT/JFace application that uses the Realm (not sure of the concept) class to run the main program as a thread. I'm trying to catch any uncaught exceptions using a try/catch block around my main code:
public static void main(String args[]) {
// ref: http://forums.instantiations.com/viewtopic.php?f=1&t=1583
Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
public void run() {
try {
PropertyConfigurator.configure("log4j.properties");
MainWindow window = new MainWindow();
window.setBlockOnOpen(true);
window.open();
Display.getCurrent().dispose();
} catch (Exception e) {
MessageDialog.openError(null, "Error", "Error occurred: " + e.getMessage());
logger.error("Error!!!", e);
e.printStackTrace();
}
}
});
}
The errors get thrown back to the window.open()
line fine, but are then passed on to Realm, so the catch
block is never reached. Here's the end of a stack trace:
at org.eclipse.jface.window.Window.runEventLoop(Window.java:825)
at org.eclipse.jface.window.Window.open(Window.java:801)
at com.ism.MainWindow$1.run(MainWindow.java:210) <-- "window.open();"
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at com.ism.MainWindow.main(MainWindow.java:204) <-- "Realm.runWithDefault....."
Tried putting a try/catch around Realm.runWithDefault
but that didn't work either.
How do I capture all of the exceptions in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
某些 UI 可运行对象在显示事件循环中抛出
Exception
。您需要设置不同的事件循环异常处理程序。 (默认情况下只是将异常打印到控制台。)例如:
或者,当然,您可以像示例中那样在顶层重新抛出并捕获。
但请注意,这是
Window
上的静态方法,因此此异常处理程序是应用程序范围。Some UI runnable is throwing an
Exception
in the display event loop. You need to set up a different event loop exception handler. (The default simply prints the exception to the console.)For example:
Or, of course, you could rethrow and catch at your top-level like in your example.
Note, however, that this is a static method on
Window
, so this exception handler is application-wide.您的
catch
块仅捕获Exception
。但是,您没有说明抛出了哪些异常但未被捕获。因此,在没有任何进一步信息的情况下,我猜测这些异常实际上是Error
。尝试使用catch (Throwable e)
而不是catch (Exception e)
。Your
catch
block is only catchingException
s. However, you don't say what exceptions are being thrown and not being caught. So, in the absence of any further information, I'm going to guess that these exceptions are in factError
s. Trycatch (Throwable e)
instead ofcatch (Exception e)
.