|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
The final step in setting up an exception handler is providing a mechanism for cleaning up the state of the method before (possibly) allowing control to be passed to a different part of the program. You do this by enclosing the cleanup code within afinallyblock.The
tryblock of thewriteListmethod that you've been working with opens aPrintWriter. The program should close that stream before allowing control to pass out of thewriteListmethod. This poses a somewhat complicated problem becausewriteList'stryblock has three different exit possibilities:The runtime system always executes the statements within the
- The
new FileWriterstatement failed and threw anIOException.- The
victor.elementAt(i)statement failed and threw anArrayIndexOutOfBoundsException.- Everything succeeded and the
tryblock exited normally.finallyblock regardless of what happens within thetryblock. Regardless of whether control exits thewriteListmethod'stryblock due to one of the three scenarios listed previously, the code within thefinallyblock will be executed.This is the
finallyblock for thewriteListmethod. It cleans up and closes the PrintWriter.finally { if (out != null) { System.out.println("Closing PrintWriter"); out.close(); } else { System.out.println("PrintWriter not open"); } }
At first the need for afinallystatement may not be immediately apparent. Programmers often ask "Is thefinallystatement really necessary or is it just sugar for my Java?" In particular, C++ programmers doubt the need for afinallystatement because C++ doesn't have one.The need for a
finallystatement is not apparent until you consider the following: how does thePrintWriterin thewriteListmethod get closed if you don't provide an exception handler for theArrayIndexOutOfBoundsExceptionand anArrayIndexOutOfBoundsExceptionoccurs? (It's easy and legal to omit an exception handler forArrayIndexOutOfBoundsExceptionbecause it's a runtime exception and the compiler won't alert you that thewriteListcontains a method call that might throw one.) The answer is that thePrintWriterdoes not get closed if anArrayIndexOutOfBoundsExceptionoccurs andwriteListdoes not provide a handler for it--unless thewriteListprovides afinallystatement.There are other benefits to using the
finallystatement. In thewriteListexample it is possible to provide for cleanup without the intervention of afinallystatement. For example, you could put the code to close thePrintWriterat the end of thetryblock and again within the exception handler forArrayIndexOutOfBoundsException, as shown here:However, this duplicates code, making the code hard to read and prone to errors if you modify the code later. For example, if you add code to thetry { . . . out.close(); // don't do this; it duplicates code } catch (ArrayIndexOutOfBoundsException e) { out.close(); // don't do this; it duplicates code System.err.println("Caught ArrayIndexOutOfBoundsException: " + e.getMessage()); } catch (IOException e) { System.err.println("Caught IOException: " + e.getMessage()); }tryblock that may throw a new type of exception, you will have to remember to close thePrintWriterwithin the new exception handler (which if you're anything like me, you are bound to forget).
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.