I wish to handle Exceptions in a meaningful way in a Swing application.
The following is inside an actionPerformed method. The UiUtils#showError displays a JOptionPane with a button that shows/hides the stack trace. ApplicationException is a custom class to translate low level Exceptions to something a user would understand.
One issue is that I'm not sure how to handle a NullPointerException that propagates up if a user doesn't select a file in the JFileChooser before this code. The exportData method purposefully checks for null on entry so no file handling is done.
Also, it seems that it would be a good approach to wrap a low level Exception (from the data layer for example). I'd like to modify ApplicationException to keep an instance instead of doing this in the showError method.
Finally, there's one thing that's bothering me and that's the possibility that a few Exceptions could happen simultaneously. I have no idea how to handle this so I'm open to any suggestions.
try {
dataService.exportData(list, selectedFile);
} catch (IOException e) {
UiUtils.showError(new ApplicationException("Input/Ouput error"), e );
} finally {
if( list == null){
UiUtils.showError(new ApplicationException("No data to export"), null );
}
if( selectedFile == null ){
UiUtils.showError(new ApplicationException("No file selected"), null );
}
}