<< Chapter < Page | Chapter >> Page > |
The catch block(s)
Continuing with what Campione and Walrath have to say:
"Next, you associate exception handlers with a try block by providing one or more catch blocks directly after the try block."
There can be no intervening code between the end of the try block and the beginning of the first catch block, and no intervening code between catch blocks.
Syntax of a catch block
The general form of a catch block is shown in Figure 7 .
Figure 7 . Syntax of a catch block. |
---|
catch(ThrowableObjectType paramName){
//Java statements to handle the// exception
}//end catch block |
The declaration for the catch block requires a single argument as shown. The syntax for the argument declaration is the same as an argumentdeclaration for a method.
Argument type specifies type of matching exception object
The argument type declares the type of exception object that a particular catch block can handle. The type must be Throwable , or a subclass of the Throwable class discussed earlier.
A parameter provides the local name
Also, as in a method declaration, there is a parameter, which is the name by which the handler can refer to the exception object. For example, in an earlierprogram, I used statements such as e.getMessage() to access an instance method of an exception object caught by the exception handler. In that case, thename of the parameter was e .
You access the instance variables and methods of exception objects the same way that you access the instance variables and methods of other objects.
Proper order of catch blocks
According to Campione and Walrath:
"The catch block contains a series of legal Java statements. These statements are executed if and when the exception handler is called. The runtimesystem calls the exception handler when the handler is the first one in the call stack whose type matches that of the exception thrown."
Therefore, the order of your exception handlers is very important, particularly if you have some handlers, which are further up the exceptionhierarchy than others.
Those handlers that are designed to handle exception types furthermost from the root of the hierarchy tree ( Throwable ) should be placed first in the list of exception handlers.
Otherwise, an exception handler designed to handle a specific type of object may be preempted by another handler whose exception type is a superclass of thattype, if the superclass exception handler appears earlier in the list of exception handlers.
Catching multiple exception types with one handler
Exception handlers that you write may be more or less specialized. In addition to writing handlers for very specialized exception objects, the Javalanguage allows you to write general exception handlers that handle multiple types of exceptions.
A hierarchy of Throwable classes
Java exceptions are Throwable objects (instances of the Throwable class or a subclass of the Throwable class).
The Java standard library contains numerous classes that are subclasses of Throwable and thus build a hierarchy of Throwable classes.
According to Campione and Walrath:
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?