Java Reference
In-Depth Information
When an exception is thrown in a method but not caught in that method, that
immediately ends the method invocation.
Be sure to note that the
throws
clause for a method is for exceptions that “get outside”
the method. If they do not get outside the method, they do not belong in the
throws
clause. If they get outside the method, they belong in the
throws
clause no matter where
they originate. If an exception is thrown in a
try
block that is inside a method definition
and is caught in a
catch
block inside the method definition, then its exception class
need not be listed in the
throws
clause. If a method definition includes an invocation of
another method and that other method can throw an exception that is not caught, then
the exception class of that exception should be placed in the
throws
clause.
Throwing an Exception Can End a Method
If a method throws an exception, and the exception is not caught inside the method, then
the method invocation ends immediately after the exception is thrown.
In Display 9.10, we have rewritten the program from Display 9.5 so that the exception
is thrown in the method
safeDivide
. The method
main
includes a call to the method
safeDivide
and puts the call in a
try
block. Because the method
safeDivide
can
throw a
DivisionByZeroException
that is not caught in the method
safeDivide
, we
need to declare this in a
throws
clause at the start of the definition of
safeDivide
. If
we set up our program in this way, the case in which nothing goes wrong is completely
isolated and easy to read. It is not even cluttered by
try
blocks and
catch
blocks.
Catch or Declare Rule
Most “ordinary” exceptions that might be thrown when a method is invoked must be
accounted for in one of two ways:
•
The possible exception can be caught in a
catch
block within the method definition.
•
The possible exception can be declared at the start of the method definition by placing
the exception class name in a
throws
clause (and letting whoever uses the method
worry about how to handle the exception).
This is known as the
Catch or Declare Rule
. In any one method, you can mix the two
alternatives, catching some exceptions and declaring others in a
throws
clause.
If you use a class that is subject to the Catch or Declare Rule and you do not follow the rule,
you will get a compiler error message. The box entitled “Checked and Unchecked Exceptions”
explains exactly which exception classes are subject to the Catch or Declare Rule.
The next subsection, entitled “Exceptions to the Catch or Declare Rule,” explains
exactly which exception classes are subject to the Catch or Declare Rule. However, the
compiler will ensure that you follow the Catch or Declare Rule when it is required. So
if you do not know whether a class is subject to the Catch or Declare Rule, you can rely
on the compiler to tell you. If you use a class that is subject to the Catch or Declare
Rule and you do not follow the rule, you will get a compiler error message.