Java Reference
In-Depth Information
If, however, the code is put in a try statement and the same exception occurs,
execution is thrown to the corresponding catch statement. The catch statement
then can execute code that instructs the program to display a descriptive message
to the user and perhaps let the user reenter the data.
As previously discussed, Java may generate the exception, as in the division
by zero example above, or you may use the keywords, throw new, to throw the
exception explicitly or intentionally. For example, if Java expects an integer
input and the user types a decimal point, a NumberFormatException displays
and the program terminates. A NumberFormatException indicates an opera-
tion attempted to use a number in an illegal format. In another example, as
shown in Table 4-6 on page 4.24, you could instruct Java to throw a
DivideByZeroException if you tried to divide a value by zero by using the
following code:
throw new DivideByZeroException();
Alternately, you might want to create a new exception type. For example, to
handle an exception when a user enters the wrong password, you could enter the
following line of code:
throw new WrongPasswordException();
The program then would call the class, WrongPasswordException, if the user
entered the wrong password. That new class must be defined by the program-
mer, however, and be accessible to the class that contains the throw statement.
The throw statement within the try statement causes execution to be trans-
ferred to the catch statement. That way, control passes to the same error-handling
routine whether the exception is caught by the JVM, as in a data type error, or
caught by the programmer testing for an invalid or unreasonable number.
As with the if statement, the try and catch statements can be nested within
any other code statements. In addition, you can have more than one catch state-
ment in the same program, or even within the same method, if you are trying to
catch multiple types of exceptions.
Catching a NumberFormatException in the getSales()
Method
In the Commission program, a valid sales amount must be numeric. The
JOptionPane input dialog box, however, does not restrict the type of data
entered in the text box. As a result, regardless of what data the user enters, the
input is stored in the variable location named answer. If, however, the user has
entered alphabetic data and the code tries to parse it, the code will generate a
NumberFormatException — an exception that must be caught. To catch the
exception, you should include the parse code inside a try statement and write
a catch statement to handle the exception.
Figure 4-17 displays the try statement and catch statement used to handle
the exception. Lines 35 through 38 show the try statement and the braces
enclosing the code that parses the answer value. Lines 39 through 42 catch the
NumberFormatException and display a JOptionPane message box. The catch
statement will execute only if a NumberFormatException occurs in line 37; when
it executes, the catch statement handles the error by displaying a message box
rather than allowing Java to terminate the program.
Search WWH ::




Custom Search