As you may know, the standard SWT main loop looks like this:
Display display = new Display();
Shell shell = new Shell(display);
...
shell.open();
w开发者_运维百科hile (!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?
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 ;) ).
精彩评论