Exceptions: When Things Go Wrong
10.5 Error Handling and Robust Program Design
Fix the Error and Resume
As an example of a problem that can be addressed as the program runs, consider the task of inputting an integer into a text field. As you have probably experienced, if a program is expecting an integer and you attempt to input something beside an integer,
a NumberFormatException is generated and the program will terminate. For example, if you enter “$55” when prompted to input an integer dollar amount, this will generate an exception when the Integer.parseInt() method is invoked. The
input string cannot be parsed into a valid int. However, this is the kind of error that can be addressed as the program is running.

Let’s design a special IntField that functions like a normal text field but accepts only integers. If the user enters a value that generates a
NumberFormatException, an error message should be printed and the user should be invited to try again. As Figure 10.13 shows, we want this special field to be a subclass of
JTextField and to inherit the basic JTextField functionality. It should have the same kind of constructors that a normal JTextField has. This leads to the definition shown in Figure 10.14.
Note that the constructor methods use super to call the JTextField constructor. For now, these two constructors should suffice. However, later we will introduce a third constructor that allows us to associate a bound with the IntField later
in this chapter.
Our IntField class needs a method that can return its contents. This method should work like JTextField.getText(), but it should return a valid integer. The getInt() method takes no parameters and will return an int, assuming
that a valid integer is typed into the
IntField. If the user enters “$55,” a NumberFormatException will be thrown by the Integer.parseInt() method. Note that getInt() declares that it throws this exception. This is not necessary because a
NumberFormatException is not a checked exception, but it makes the code clearer.
Where and how should this exception be handled? The exception cannot easily be handled within the getInt() method. This method has to return an integer value. If the user types in a non-integer, there’s no way to return a valid value. Therefore,
it’s better just to throw the exception to the calling method, where it can be handled more easily.
In a GUI application or applet, the calling method is likely to be an
actionPerformed() method, such as the following:
The call to getInt() is embedded in a try/catch block. This leads to the design summarized in Figure 10.15. The IntField throws an exception that is caught by the GUI, which then displays an error message.
If the user inputs a valid integer, the program will just report a message that displays the value. A more real-world example would make a more significant use of the value. On the other hand, if the user types an erroneous value, the program will pop
up the dialog box shown in Figure 10.16. (See the “From the Library” section of this chapter for more on dialog boxes.) When the user clicks the OK button, the program will resume normal execution, so that when an exception is raised, the enter value
is not used, and no harm is done by an erroneous value. The user can try again to input a valid integer. Note that the finally clause repaints
the GUI. In this case, repainting would display the appropriate message
on the applet or the application.
This is an example of what we might call defensive design. Defensive design is when we anticipate a possible input error and take steps to ensure that a bad value is not propagated throughout the program.
Admittedly, the sense in which the error here is “fixed” is simply that
the user’s original input is ignored and reentered. This is a legitimate
and simple course of action for this particular situation. It is far preferable to ignoring the exception. If the program does not handle this exception itself, Java will catch it and will print a stack trace and terminate the
program. That would not be a very user-friendly interface!
Clearly, this is the type of exceptional condition that should be anticipated during program design. If this happens to be a program designed
exclusively for your own use, then this type of exception handling might
be unnecessary. But if the program is meant to be used by others, it is important that the program be able to handle erroneous user input without
crashing.