<< Chapter < Page | Chapter >> Page > |
Since a pair of curly brackets is required to define a try block, the scope of any variables or objects declared inside the try block is limited to the try block.
While this is not an insurmountable problem, it may require you to modify your programming style in ways that you find distasteful. In particular, if youneed to access a variable both within and outside the try block, you must declare it before entering the try block.
The process in more detail
Now that you have seen some sample programs to help you visualize the process, lets discuss the process in more detail.
The try block
According to Campione and Walrath,
"The first step in writing any exception handler is putting the Java statements within which an exception can occur into a try block. The try blockis said to govern the statements enclosed within it and defines the scope of any exception handlers (established by subsequent catch blocks) associated with it."
Note that the terminology being used by Campione and Walrath treats the catch block as the "exception handler" and treats the try block as something that precedes one or more exception handlers. I don'tdisagree with their terminology. I mention it only for the purpose of avoiding confusion over terminology.
The syntax of a try block
The general syntax of a try block, as you saw in the previous program, has the keyword try followed by one or more statements enclosed in a pair of matching curly brackets, as shown in Figure 6 .
Figure 6 . Syntax of a try block. |
---|
try{
//java statements}//end try block |
Single statement and multiple exceptions
You may have more than one statement that can throw one or more exceptions and you will need to deal with all of them.
You could put each such statement that might throw exceptions within its own try block and provide separate exception handlers for each try block.
(Note that some statements, particularly those that call other methods, could potentially throw many different types of exceptions.)
Thus a try block consisting of a single statement might require manydifferent exception handlers or catch blocks following it.
Multiple statements and multiple exceptions
You could put all or several of the statements that might throw exceptions within a single try block and associate multiple exception handlers with it. There are a number of practical issues involved here, and only you candecide in any particular instance which approach would be best.
The catch blocks must follow the try block
However you decide to do it, the exception handlers associated with a try block must be placed immediately following their associated try block. If an exception occurs within the try block, that exception is handled by the appropriate exception handler associated with the try block. If there is no appropriate exception handler associated with the try block, the system attempts to find an appropriate exception handler in the nextmethod up the call stack.
A try block must be accompanied by at least one catch block (or one finally block). Otherwise, a compiler error that reads something like 'try' without 'catch' or 'finally' will occur.
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?