Java Reference
In-Depth Information
34
35
try
36
{
37
sales = Double .parseDouble ( answer ) ;
38
}
39
catch ( NumberFormatException e )
40
{
41
JOptionPane.showMessageDialog ( null , "Your entry was not in the proper format." ,
"Error" ,JOptionPane.INFORMATION_MESSAGE ) ;
42
}
43
return sales;
FIGURE 4-17
The following step enters the try and catch statements.
To Code the try and catch Statements
1. Enter lines 35 through 36 and lines 38 through 42 as shown in
Figure 4-18. Use proper spacing and indentation.
The try and catch statements are displayed in the TextPad coding window
(Figure 4-18). Line 37 is indented within the braces of the try statement.
Line 41 wraps. If your display does not wrap, click Word Wrap on the
Configure menu.
try statement
catch
statement
FIGURE 4-18
The try and catch statements may be followed by an optional finally state-
ment, which is placed after the catch statement. The finally statement typically
contains code to perform any cleanup that might be necessary after executing the
try statement and catch statement. The finally statement always is executed,
regardless of whether the try statement generates an exception. The most com-
mon usage of the finally statement is to release a resource specifically allocated
for use by a method, such as an open file.
Search WWH ::




Custom Search