Java Reference
In-Depth Information
Here is the output produced by the program:
As the output shows, no matter how the
try
block is exited, the
finally
block is executed.
Using throws
In some cases, if a method generates an exception that it does not handle, it must declare
that exception in a
throws
clause. Here is the general form of a method that includes a
throws
clause:
Here,
except-list
is a comma-separated list of exceptions that the method might throw out-
side of itself.
You might be wondering why you did not need to specify a
throws
clause for some of
the preceding examples, which threw exceptions outside of methods. The answer is that
exceptions that are subclasses of
Error
or
RuntimeException
don't need to be specified
in a
throws
list. Java simply assumes that a method may throw one. All other types of ex-
ceptions
do
need to be declared. Failure to do so causes a compile-time error.
Actually, you saw an example of a
throws
clause earlier in this topic. As you will recall,
when performing keyboard input, you needed to add the clause