Java Reference
In-Depth Information
code that follows the try keyword, as well as the try keyword.] The statements that read
the integers from the keyboard (lines 25 and 27) each use method nextInt to read an int
value. Method nextInt throws an InputMismatchException if the value read in is not an
integer.
The division that can cause an ArithmeticException is not performed in the try
block. Rather, the call to method quotient (line 29) invokes the code that attempts the
division (line 12); the JVM throws an ArithmeticException object when the denomi-
nator is zero.
Software Engineering Observation 11.1
Exceptions may surface through explicitly mentioned code in a try block, through deeply
nested method calls initiated by code in a try block or from the Java Virtual Machine as
it executes Java bytecodes.
Catching Exceptions
The try block in this example is followed by two catch blocks—one that handles an In-
putMismatchException (lines 34-41) and one that handles an ArithmeticException
(lines 42-47). A catch block (also called a catch clause or exception handler ) catches (i.e.,
receives) and handles an exception. A catch block begins with the keyword catch followed
by a parameter in parentheses (called the exception parameter , discussed shortly) and a
block of code enclosed in curly braces. [ Note: The term “ catch clause” is sometimes used
to refer to the keyword catch followed by a block of code, whereas the term “ catch block”
refers to only the block of code following the catch keyword, but not including it. For
simplicity, we use the term “ catch block” to refer to the block of code following the catch
keyword, as well as the keyword itself.]
At least one catch block or a finally block (discussed in Section 11.6) must imme-
diately follow the try block. Each catch block specifies in parentheses an exception
parameter that identifies the exception type the handler can process. When an exception
occurs in a try block, the catch block that executes is the first one whose type matches the
type of the exception that occurred (i.e., the type in the catch block matches the thrown
exception type exactly or is a direct or indirect superclass of it). The exception parameter's
name enables the catch block to interact with a caught exception object—e.g., to implic-
itly invoke the caught exception's toString method (as in lines 37 and 44), which displays
basic information about the exception. Notice that we use the System.err (standard error
stream) object to output error messages. By default, System.err 's print methods, like
those of System.out , display data to the command prompt .
Line 38 of the first catch block calls Scanner method nextLine . Because an Input-
MismatchException occurred, the call to method nextInt never successfully read in the
user's data—so we read that input with a call to method nextLine . We do not do anything
with the input at this point, because we know that it's invalid . Each catch block displays
an error message and asks the user to try again. After either catch block terminates, the
user is prompted for input. We'll soon take a deeper look at how this flow of control works
in exception handling.
Common Programming Error 11.1
It's a syntax error to place code between a try block and its corresponding catch blocks.
 
Search WWH ::




Custom Search