The previous sections described how to construct thetry,catch, andfinallycode blocks for thewriteListmethod in theListOfNumbersclass. Now, let's walk through the code and investigate what can happen.When all the components are put together, the
writeListmethod looks like the following.As mentioned previously, this method'spublic void writeList() {
PrintWriter out = null;
try {
System.out.println("Entering try statement");
out = new PrintWriter(
new FileWriter("OutFile.txt"));
for (int i = 0; i < SIZE; i++)
out.println("Value at: " + i + " = "
+ vector.elementAt(i));
} catch (ArrayIndexOutOfBoundsException e) {
System.err.println("Caught "
+ "ArrayIndexOutOfBoundsException: "
+ e.getMessage());
} catch (IOException e) {
System.err.println("Caught IOException: "
+ e.getMessage());
} finally {
if (out != null) {
System.out.println("Closing PrintWriter");
out.close();
}
else {
System.out.println("PrintWriter not open");
}
}
}tryblock has three different exit possibilities; here are two of them.Let's look at what happens in the
- Code in the
trystatement fails and throws an exception. This could be anIOExceptioncaused by thenew FileWriterstatement or anArrayIndexOutOfBoundsExceptioncaused by a wrong index value in theforloop.- Everything succeeds and the
trystatement exits normally.writeListmethod during these two exit possibilities.
Scenario 1: An Exception Occurs
The statement that creates aFileWritercan fail for a number of reasons. For example, the constructor for theFileWriterthrows anIOExceptionif the program cannot create or write to the file indicated.When
FileWriterthrows anIOException, the runtime system immediately stops executing thetryblock; method calls being executed are not completed. The runtime system then starts searching at the top of the method call stack for an appropriate exception handler. In this example, when theIOExceptionoccurs, theFileWriterconstructor is at the top of the call stack. However, theFileWriterconstructor doesn't have an appropriate exception handler, so the runtime system checks the next method — thewriteListmethod — in the method call stack. ThewriteListmethod has two exception handlers: one forIOExceptionand one forArrayIndexOutOfBoundsException.The runtime system checks
writeList's handlers in the order in which they appear after thetrystatement. The argument to the first exception handler isArrayIndexOutOfBoundsException. This does not match the type of exception thrown, so the runtime system checks the next exception handler —IOException. This matches the type of exception that was thrown, so the runtime system ends its search for an appropriate exception handler. Now that the runtime has found an appropriate handler, the code in thatcatchblock is executed.After the exception handler executes, the runtime system passes control to the
finallyblock. Code in thefinallyblock executes regardless of the exception caught above it. In this scenario, theFileWriterwas never opened and doesn't need to be closed. After thefinallyblock finishes executing, the program continues with the first statement after thefinallyblock.Here's the complete output from the
ListOfNumbersprogram that appears when anIOExceptionis thrown.The boldface code in the following listing shows the statements that get executed during this scenario:Entering try statement
Caught IOException: OutFile.txt
PrintWriter not openpublic void writeList() {
PrintWriter out = null;
try {
System.out.println("Entering try statement");
out = new PrintWriter(
new FileWriter("OutFile.txt"));
for (int i = 0; i < SIZE; i++)
out.println("Value at: " + i
+ " = " + vector.elementAt(i));
} catch (ArrayIndexOutOfBoundsException e) {
System.err.println("Caught "
+ "ArrayIndexOutOfBoundsException: "
+ e.getMessage());
} catch (IOException e) {
System.err.println("Caught IOException: "
+ e.getMessage());
} finally {
if (out != null) {
System.out.println("Closing PrintWriter");
out.close();
}
else {
System.out.println("PrintWriter not open");
}
}
}
Scenario 2: The try Block Exits Normally
In this scenario, all the statements within the scope of thetryblock execute successfully and throw no exceptions. Execution falls off the end of thetryblock, and the runtime system passes control to thefinallyblock. Because everything was successful, thePrintWriteris open when control reaches thefinallyblock, which closes thePrintWriter. Again, after thefinallyblock finishes executing, the program continues with the first statement after thefinallyblock.Here is the output from the
ListOfNumbersprogram when no exceptions are thrown.The boldface code in the following sample shows the statements that get executed during this scenario.Entering try statement
Closing PrintWriterpublic void writeList() {
PrintWriter out = null;
try {
System.out.println("Entering try statement");
out = new PrintWriter(
new FileWriter("OutFile.txt"));
for (int i = 0; i < SIZE; i++)
out.println("Value at: " + i + " = "
+ vector.elementAt(i));
} catch (ArrayIndexOutOfBoundsException e) {
System.err.println("Caught "
+ "ArrayIndexOutOfBoundsException: "
+ e.getMessage());
} catch (IOException e) {
System.err.println("Caught IOException: "
+ e.getMessage());
} finally {
if (out != null) {
System.out.println("Closing PrintWriter");
out.close();
}
else {
System.out.println("PrintWriter not open");
}
}
}
No comments:
Post a Comment