Java Reference
In-Depth Information
The effect of an exception on the point in the program that called the problem method is a lit-
tle more complex. In particular, the full effect depends upon whether or not any code has been
written to catch the exception. Consider the following contrived call to getDetails :
AddressDetails details = contacts.getDetails(null);
// The following statement will not be reached.
String phone = details.getPhone();
We can say that, in all cases, the execution of these statements will be left incomplete; the
exception thrown by getDetails will interrupt the execution of the first statement, and no
assignment will be made to the details variable. As a result, the second statement will not be
executed either.
This neatly illustrates the power of exceptions to prevent a client from carrying on regardless of
the fact that a problem has arisen. What actually happens next depends upon whether or not the
exception is caught. If it isn't caught, then the program will simply terminate with an indication
that an uncaught IllegalArgumentException has been thrown. We shall discuss how to
catch an exception in Section 12.5.2.
12.4.4
Using unchecked exceptions
Unchecked exceptions are the easiest to use from a programmer's point of view, because the
compiler enforces few rules on their use. This is the meaning of “unchecked”: the compiler does
not apply special checks, either on the method in which an unchecked exception is thrown or on
the place from which the method is called. An exception class is unchecked if it is a subclass of
the RuntimeException class, defined in the java.lang package. All of the examples we have
used so far to illustrate exception throwing have involved unchecked exceptions. So there is little
further to add here about how to throw an unchecked exception—simply use a throw statement.
Concept:
An unchecked
exception is a
type of exception
whose use will not
require checks from
the compiler.
If we also follow the convention that unchecked exceptions should be used in those situations
where we expect the result to be program termination—i.e., the exception is not going to be
caught—then there is also nothing further to be discussed about what the method's caller should
do, because it will do nothing and let the program fail. However, if there is a need to catch an
unchecked exception, then an exception handler can be written for it, exactly as for a checked
exception. How to do this is described in Section 12.5.2.
We have already seen use of the unchecked IllegalArgumentException . This is thrown by a
constructor or method to indicate that its parameter values are inappropriate. For instance, the get-
Details method might also choose to throw this if the key string passed to it is blank (Code 12.6).
Code 12.6
Checking for an illegal
parameter value
/**
* Look up a name or phone number and return the
* corresponding contact details.
* @param key The name or number to be looked up.
* @throws IllegalArgumentException if the key is invalid.
* @return The details corresponding to the key,
* or null if there are none matching.
*/
 
Search WWH ::




Custom Search