Java SWT:在异常处理程序中包装主循环?

时间:2011-01-30 14:21:21

标签: java swt

如您所知,标准SWT主循环如下所示:

Display display = new Display();
Shell shell = new Shell(display);
...
shell.open();
while (!shell.isDisposed()) {
  if (!display.readAndDispatch()) {
    display.sleep();
  }
}
display.dispose();

最近,我与一位同事讨论过将主循环包装在try-catch中是否有意义,如下所示:

Display display = new Display();
Shell shell = new Shell(display);
...
shell.open();
while (!shell.isDisposed()) {
  try {
    if (!display.readAndDispatch()) {
      display.sleep();
    }
  } catch (RuntimeException e) {
    // TODO Implement exception handler
  }
}
display.dispose();

我的同事说这样做,如果GUI线程发生崩溃,你不必立即关闭应用程序,因此用户可能有机会在关闭程序之前保存他的数据。

那么,您怎么看?做这样的事情有意义吗?

1 个答案:

答案 0 :(得分:6)

这样做非常好,因为您的GUI代码中可能会出现任何异常。我们在这个位置有一个通用的BugReport发送者,我喜欢它,因为什么都不会丢失,并且应用程序在错误报告之后继续工作(主要是;))。