|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
The previous sections describe how to construct thetry,catch, andfinallycode blocks for thewriteListexample. Now, let's walk through the code and investigate what can happen.When all of the components are put together, the
writeListmethod looks like this:As mentioned previously, thepublic 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 + " = " + victor.elementAt(i)); } } catch (FileNotFoundException e) { System.err.println("Caught: FileNotFoundException: " + e.getMessage()); throw new RuntimeException(e); } 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 in this method has three different exit possibilities.Let’s look at what happens in the
- The
new FileWriterstatement fails and throws anIOException.- The
victor.elementAt(i)statement fails and throws anArrayIndexOutOfBoundsException.- Everything succeeds and the
trystatement exits normally.writeListmethod during the two of these exit possibilities.
IOException OccursThe statement that creates aPrintWritercan fail for a number of reasons. For example, the constructor forPrintWriterthrows anFileNotFoundExceptionif the given string does not denote an existing, writable regular file and a new regular file of that name cannot be created, or the file system is full, or the directory for the file doesn’t exist.When
PrintWriterthrows aFileNotFoundException, the runtime system immediately stops executing thetryblock. The runtime system then starts searching at the top of the method call stack for an appropriate exception handler. In this example, when theFileNotFoundExceptionoccurs, thePrintWriterconstructor is at the top of the call stack. However, thePrintWriterconstructor doesn’t have an appropriate exception handler, so the runtime system checks the next method in the method call stack—thewriteListmethod. ThewriteListmethod has two exception handlers: one forFileNotFoundExceptionand one forIOException.The runtime system checks
writeList’s handlers in the order in which they appear after thetrystatement. The argument to the first exception handler isFileNotFoundException. Since this matches the type of exception that was thrown, the runtime system ends its search for an appropriate exception handler. Now that the runtime has found an appropriate handler, the code in thatcatchclause is executed.After the exception handler has executed, the runtime system passes control to the
finallyblock. In this scenario, thePrintWriterwas never opened and doesn’t need to be closed. After thefinallyblock has completed executing, the program continues with the first statement after thefinallyblock.Here’s the complete output that you see from the
ListOfNumbersprogram when aFileNotFoundExceptionis thrown:The boldface code in the following listing shows the statements that get executed during this scenario:[** verify this is correct output Entering try statement Caught IOException: OutFile.txt PrintWriter not open ***]public 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 + " = " + victor.elementAt(i)); } } catch (FileNotFoundException e) { System.err.println("Caught FileNotFoundException: " + 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"); } } }
try Block Exits NormallyIn 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 has completed 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 code 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 + " = " + victor.elementAt(i)); } } catch (FileNotFoundException e) { System.err.println("Caught FileNotFoundException: " + 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"); } } }
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.