Java Reference
In-Depth Information
How It Works
All the
try
,
catch
, and
finally
blocks in the example have output statements so we can trace the
sequence of execution.
Within the
divide()
method the code in the
try
block can throw an arithmetic exception if the
element
array[index + 1]
of the array passed to it is 0. It can also throw an
ArrayIndexOutOfBounds
exception in the
try
block if the index value passed to it is negative, or it
results in
index + 2
being beyond the array limits. Both these exceptions are caught by one or other
of the
catch
blocks so they will not be apparent in the calling method
main()
.
Note, however, that the last statement in
divide()
can also throw an index-out-of-bounds exception:
return array[index+2];
This statement is outside the
try
block so the exception will not be caught. The exception will therefore be
thrown by the method when it is called in
main()
. However, we aren't obliged to declare that the
divide()
method throws this exception because the
ArrayIndexOutOfBoundsException
class is a
subclass of
RuntimeException
, and is therefore exempted from the obligation to deal with it.
The
main()
method has two
try
blocks. The first
try
block encloses three calls to the
divide()
method. The first call will execute without error; the second call will cause an arithmetic exception in
the method; and the third call will cause an index-out-of-bounds exception. There are two
catch
blocks for the first
try
block in
main()
to deal with these two potential exceptions.
The
read()
method in the second
try
block in
main()
can cause an I/O exception to be thrown.
Since this is one of the exceptions that the compiler will check for, we must either put the statement that
calls the
read()
method in a
try
block, and have a
catch
block to deal with the exception, or
declare that
main()
throws the
IOException
exception. If we don't do one or the other, the program
will not compile.
Using the
read()
method in this way has the effect of pausing the program until the
Enter
key is
pressed. We'll be looking at
read()
, and other methods for I/O operations, in the next four chapters.
The class
IOException
is in the package
java.io
, so we need the
import
statement for this class
because we refer to it in the
catch
block. Remember that only classes defined in
java.lang
are
included in your program automatically.
Normal Execution of a Method
The first line of output from the example,
TryBlockTest
, indicates that execution of the
try
block in
main()
has begun. The next block of four lines of output from the example is the result of a
straightforward execution of the method
divide()
. No exceptions occur in
divide()
, so no
catch
blocks are executed.
The code at the end of the method
divide()
, following the
catch
blocks, isn't executed because the
return
statement in the
try
block ends the execution of the method. However, the
finally
block in
divide()
is executed before the return to the calling method occurs. If you comment out the
return
statement at the end of the
divide()
method's
try
block and run the example again, the code that
follows the
finally
block will be executed.