Java Reference
In-Depth Information
Figure 7-1.
However, some of the stack trace will make sense to you. In this case, it is the fourth line that indicates that line 42
within c7.EnterEmpInfo (in the actionPerformed method) was the statement that triggered the problem.
Now, was it really the Java statement that caused the problem, or was the data passed to the statement the
problem? It was the data. Because the value being parsed contained a text character ($), the parse method failed. So
what is a programmer to do? Fortunately, programmers can add code to handle (i.e., catch) exceptions when they
occur (i.e., are thrown).
Identifying an Exception
Programmers can determine which exceptions may be generated (thrown) through testing. In other words, a
programmer could type in a variety of incorrect information to generate exceptions. However, this is a very time
consuming technique and not very thorough.
Fortunately, the online documentation for class methods lists the exceptions that can be thrown (i.e., generated).
Figure 7-2 shows the online documentation for the Double class's parseDouble method. (Notice that the lower-left-hand
pane shows that the Double class was selected and in the right-hand pane, we have scrolled down to the parseDouble
method.) The exceptions that can be thrown are identified in both the method header and the text explanation
below the header. So, based on the documentation, we now know that every parseDouble statement can generate a
NumberFormatException object.
 
Search WWH ::




Custom Search