Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
The previous sections describe how to construct thetry
,catch
, andfinally
code blocks for thewriteList
method in theListOfNumbers
class. Now, let's walk through the code and investigate what can happen.When all of the components are put together, the
writeList
method 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 (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"); } } }try
block in this method has three different exit possibilities.Let�s look at what happens in the
- Code in the
try
statement fails and throws an exception. This could be anIOException
caused by thenew FileWriter
statement, anArrayIndexOutOfBoundsException
caused by a wrong index value in thefor
loop, or aRuntimeException
caused by an error in the Java runtime system.- Everything succeeds and the
try
statement exits normally.writeList
method during the two of these exit possibilities.
The statement that creates aFileWriter
can fail for a number of reasons. For example, the constructor for theFileWriter
throws anIOException
if the program can not create or write to the file indicated.When
FileWriter
throws aIOException
, the runtime system immediately stops executing thetry
block. The 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 theIOException
occurs, theFileWriter
constructor is at the top of the call stack. However, theFileWriter
constructor doesn�t have an appropriate exception handler, so the runtime system checks the next method in the method call stack thewriteList
method. ThewriteList
method has two exception handlers: one forIOException
and one forArrayIndexOutOfBoundsException
.The runtime system checks
writeList
�s handlers in the order in which they appear after thetry
statement. 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 thatcatch
block is executed.After the exception handler has executed, the runtime system passes control to the
finally
block. Code in thefinally
block executes regardless of the exception caught above it. In this scenario, theFileWriter
was never opened and doesn�t need to be closed. After thefinally
block has completed executing, the program continues with the first statement after thefinally
block.Here�s the complete output that you see from the
ListOfNumbers
program when anIOException
is 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 + " = " + victor.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"); } } }
In this scenario, all the statements within the scope of thetry
block execute successfully and throw no exceptions. Execution falls off the end of thetry
block, and the runtime system passes control to thefinally
block. Because everything was successful, thePrintWriter
is open when control reaches thefinally
block, which closes thePrintWriter
. Again, after thefinally
block has completed executing, the program continues with the first statement after thefinally
block.Here is the output from the
ListOfNumbers
program 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 (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"); } } }
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.