Page 591 - Introduction to Programming with Java: A Problem Solving Approach
P. 591
14.3 Using try and catch Blocks to Handle “Dangerous” Method Calls 557
Note the InputMismatchException above. That’s the type of exception that’s generated when a user enters a non-integer in response to a nextInt method call. Note the exception message. Exception mes- sages can be annoying, but they serve a useful purpose. They provide information about what’s gone wrong. Toward the end of this chapter, we cover exception message details. But first a more important issue—how to avoid getting ugly exception messages in the first place. Let us begin.
14.3 Using try and catch Blocks to Handle “Dangerous” Method Calls
Some method calls, like nextInt, are dangerous in that they can lead to exceptions, and exceptions can lead to program crashes. By the way, “dangerous” is not a standard exception handling term, but we’ll use it because it helps with explanations. In this section, we describe how to use try and catch blocks to fend off exception messages and program crashes. Use a try block to “try” out one or more dangerous method calls. If there’s a problem with the dangerous method call(s), the JVM jumps to a catch block and the JVM executes the catch block’s enclosed statements. Drawing an analogy, a try block is like a circus trapeze act. A trapeze act contains one or more dangerous stunts (e.g., a triple flip, a triple twist). The dangerous stunts are like dangerous method calls. If something goes wrong with one of the stunts and an acrobat falls, there’s a net to catch the acrobat. Likewise, if something goes wrong with one of the danger- ous method calls, control passes to a catch block. If nothing goes wrong with the trapeze stunts, the net isn’t used at all. Likewise, if nothing goes wrong with the dangerous method calls, the catch block isn’t used at all.
Apago PDF Enhancer
Syntax and Semantics
Here’sthesyntaxfortryandcatch blocks: try
{
<statement(s)>
catch (<exception-class> <parameter>)
a
lo
a
“
“d
d
a
an
n
g
l
y
ge
y,
,o
on
n
e
eo
or
rm
m
o
or
re
eo
o
f
The exception class should match the type of exception that the try block might throw.
f
t
t
h
he
dc
e
s
ca
s
e
a
l
e
s
l
l
o
r
st
t
a
r
c
at
t
co
e
em
on
m
e
n
s
s
t
en
t
r
n
t
}
ru
t
s
u
c
sw
ct
t
wi
o
or
il
r
c
l
l
b
e
c
a
a
l
e
er
ro
ou
u
s
s”
”
A
A
P
PI
I
m
m
e
et
t
h
h
o
o
d
ll
l
.
.
{
<error-handling-code>
N
lb
No
o
r
r
m
m
a
a
l
ll
}
As shown above, a try block and its associated catch block (or multiple catch blocks, which we’ll ad- dress later) must be contiguous. You can put other statements before the try block or after the (last) catch block, but not between them. Note the parameter in the catch block’s heading. We’ll explain catch block parameters in the context of the following example program.
See Figure 14.1’s LuckyNumber program. Note how the try and catch blocks follow the syntax pattern shown above. Within the try block, the nextInt method call tries to convert a user entry to an integer. For the conversion to work, the user entry must contain only digits and an optional preceding minus sign. If the user entry conforms to that format, the JVM assigns the user entry to the num variable, skips the catch block, and continues with the code below the catch block. If the user entry does not con- form to that format, an exception occurs. If an exception occurs, the JVM immediately exits from the try block and instantiates an exception object—an object that contains information about the exception event.