Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java SWT: Wrap main loop in exception handler?

Tags:

java

swt

As you may know, the standard SWT main loop looks like this:

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

Recently, I had an argument with a colleague about whether it would make sense to wrap the main loop in a try-catch, like so:

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();

My colleague says doing it this way you won't have to immediately shut down the application if a crash in the GUI thread occurs, hence the user may have a chance to save his data before closing the program.

So, what do you think? Does it make sense to do such a thing?

like image 928
python dude Avatar asked Jan 30 '11 14:01

python dude


1 Answers

It is a very good style to do so, because any Exception can occur in your GUI code. We have a generic BugReport sender at this location, and I love it, because nothing gets lost, and the app continues to work after the bug report (mostly ;) ).

like image 159
Daniel Avatar answered Oct 22 '22 03:10

Daniel