Java Reference
In-Depth Information
we discuss the
finally
block in Section 11.6). Now the condition for the
do
…
while
loop
is
false
, and method
main
ends.
The
try
block and its corresponding
catch
and/or
finally
blocks form a
try
state-
ment
. Do not confuse the terms “
try
block” and “
try
statement”—the latter includes the
try
block as well as the following
catch
blocks and/or
finally
block.
As with any other block of code, when a
try
block terminates,
local variables
declared
in the block
go out of scope
and are no longer accessible; thus, the local variables of a
try
block are not accessible in the corresponding
catch
blocks. When a
catch
block
termi-
nates
,
local variables
declared within the
catch
block (including the exception parameter
of that
catch
block) also
go out of scope
and are
destroyed
. Any remaining
catch
blocks in
the
try
statement are
ignored
, and execution resumes at the first line of code after the
try
…
catch
sequence—this will be a
finally
block, if one is present.
Using the
throws
Clause
In method
quotient
(Fig. 11.3, lines 9-13), line 10 is known as a
throws
clause
. It spec-
ifies the exceptions the method
might
throw if problems occur. This clause, which must
appear after the method's parameter list and before the body, contains a comma-separated
list of the exception types. Such exceptions may be thrown by statements in the method's
body or by methods called from there. We've added the
throws
clause to this application
to indicate that this method might throw an
ArithmeticException
. Method
quotient
's
callers are thus informed that the method might throw an
ArithmeticException
. Some
exception types, such as
ArithmeticException
, are not required to be listed in the
throws
clause. For those that are, the method can throw exceptions that have the
is-a
relationship
with the classes listed in the
throws
clause. You'll learn more about this in Section 11.5.
Error-Prevention Tip 11.1
Read the online API documentation for a method before using it in a program. The docu-
mentation specifies the exceptions thrown by the method (if any) and indicates reasons why
such exceptions may occur. Next, read the online API documentation for the specified excep-
tion classes. The documentation for an exception class typically contains potential reasons
that such exceptions occur. Finally, provide for handling those exceptions in your program.
When line 12 executes, if the
denominator
is zero, the JVM throws an
Arithmetic-
Exception
object. This object will be caught by the
catch
block at lines 42-47, which dis-
plays basic information about the exception by
implicitly
invoking the exception's
toString
method, then asks the user to try again.
If the
denominator
is not zero, method
quotient
performs the division and returns
the result to the point of invocation of method
quotient
in the
try
block (line 29). Lines
30-31 display the result of the calculation and line 32 sets
continueLoop
to
false
. In this
case, the
try
block completes successfully, so the program skips the
catch
blocks and fails
the condition at line 48, and method
main
completes execution normally.
When
quotient
throws an
ArithmeticException
,
quotient
terminates
and does
not
return a value, and
quotient
's
local variables go out of scope
(and are destroyed). If
quo-
tient
contained local variables that were references to objects and there were no other ref-
erences to those objects, the objects would be marked for
garbage collection
. Also, when an
exception occurs, the
try
block from which
quotient
was called
terminates
before lines
30-32 can execute. Here, too, if local variables were created in the
try
block prior to the
exception's being thrown, these variables would go out of scope.