Java (SWT/JFace) - 处理 Runnable 内的所有异常

发布于 2024-12-09 04:00:53 字数 1412 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

星軌x 2024-12-16 04:00:53

某些 UI 可运行对象在显示事件循环中抛出Exception。您需要设置不同的事件循环异常处理程序。 (默认情况下只是将异常打印到控制台。)

例如:

Window.setExceptionHandler(new Window.IExceptionHandler() {
    public void handleException(Throwable error) {
        MessageDialog.openError(null, "Error", "Error: " + error.getMessage());
    }
});

或者,当然,您可以像示例中那样在顶层重新抛出并捕获。

但请注意,这是 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:

Window.setExceptionHandler(new Window.IExceptionHandler() {
    public void handleException(Throwable error) {
        MessageDialog.openError(null, "Error", "Error: " + error.getMessage());
    }
});

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.

指尖上的星空 2024-12-16 04:00:53

您的catch块仅捕获Exception。但是,您没有说明抛出了哪些异常但未被捕获。因此,在没有任何进一步信息的情况下,我猜测这些异常实际上是Error。尝试使用 catch (Throwable e) 而不是 catch (Exception e)

Your catch block is only catching Exceptions. 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 fact Errors. Try catch (Throwable e) instead of catch (Exception e).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文