Java Reference
In-Depth Information
Figure 8-28. The GUI created by the MainWindow class and the DVDScreen class
Just like all other action handlers in the Denny's DVDs application that communicate
with the GUIController , the search action shown in Listing 8-24 must be ready to receive a
GUIControllerException from the GUIController . This particular method, however, handles
the exception a little differently. The GUIControllerException that is thrown from the find
(String query) method may contain a PatternSyntaxException as its cause. This particular
exception is thrown if the user enters a search string that is not a properly formatted regular
expression. In this case, the exception message may contain important information for the
user pertaining to the incorrect syntax of the search. In order to present this information to
the user, J2SE's exception chaining facility is put to use. When the actionPerformed() method
in Listing 8-24 catches an exception, it checks the exception's cause to see if it is of type
PatternSyntaxException . If it is this type, the exception message is added to the message that
is passed to the handleException method. The end result is a very useful error dialog box that
looks like the one shown in Figure 8-29.
Listing 8-24. The SearchDVD Event Handler
private class SearchDVD implements ActionListener {
public void actionPerformed (ActionEvent ae) {
previousSearchString = searchField.getText();
try {
tableData = controller.find(previousSearchString);
setupTable();
} catch (GUIControllerException gce) {
// Inspect the exception chain
Throwable rootException = gce.getCause();
String msg = "Search operation failed.";
// If a syntax error occurred, get the message
if (rootException instanceof PatternSyntaxException) {
msg += ("\n" + rootException.getMessage());
}
ApplicationRunner.handleException(msg);
Search WWH ::




Custom Search