Page 616 - Introduction to Programming with Java: A Problem Solving Approach
P. 616

                582 Chapter 14 Exception Handling
This time, since we’re returning a value, it’s more convenient to transfer the exception handling work back tothecallingmethod.Wedothatbyputtingtryandcatch blocksinthecallingmethodandbyput- ting a throws clause in the removeStudent method’s heading. Here’s the heading:
public String removeStudent(int index)
throws IndexOutOfBoundsException
Adding the throws clause reminds the compiler that the method might throw an unhandled exception. The throws clause is required if the unhandled exception is a checked exception, and it’s just recommended if the unhandled exception is an unchecked exception. Since the IndexOutOfBoundsException is an unchecked exception, it’s legal to omit the above throws clause. But it’s good style to include it be- cause it provides valuable self-documentation. If a programmer later on wants to use the removeStudent method, the throws clause warns the programmer to provide a “remote” try-catch mechanism to han- dle the IndexOutOfBoundsException when calling removeStudent.
To see how to implement this “remote” try-catch mechanism, look at the StudentList2Driver class in Figure 14.16. It displays a list of students, asks the user which student should be removed, and at- tempts to remove that student. If the removeStudent method call fails, the catch block handles the thrown exception, and the program asks the user again which student should be removed.
The finally Block
In implementing an exception handler, you’ll sometimes want to provide “cleanup code” that’s executed
regardless of whether an exception is thrown. Suppose you open a file and attempt to write to it. The write
Apago PDF Enhancer
operation may or may not throw an exception. Either way, you should close the file when you’re done. If you forget to close the file, then system resources remain tied up servicing the open file, and that causes system performance to degrade. Closing a file is an example of cleanup code. If you handle the cleanup locally (e.g., the write and close operations are in the same method), then the cleanup is straightforward. Just place the cleanup code below the try and catch blocks, and the JVM executes it regardless of whether an ex- ception is thrown. But if you transfer the exception handling work with a throws clause, the cleanup is slightly more involved.
If you handle an exception with a throws clause, and you need to provide clean-up code regardless of whether an exception is thrown, use a finally block. A finally block is associated with a particular
6 tryblockand,assuch,itshouldbeplacedimmediatelyafteratryblock. IftheJVMthrowsanexception
within the try block, the JVM immediately jumps to the finally block, executes it, and then throws the exception back to the calling module. If the JVM does not throw an exception within the try block, the JVM finishes the try block and then executes the finally block.
The writeToFile method in Figure 14.17 illustrates the finally block. The method opens a file and writes a test message to the file. Specifically, the PrintWriter constructor call opens a file named testFile.txt. The fileOut.printf call writes “This is a test.” to the opened file. Then the file is closed by fileOut.close. Since the fileOut.close call is within a finally block, it executes regardless of whether an exception is thrown.
6 It’s legal to insert a catch block(s) between the try and finally blocks, but that can lead to confusing code. We recommend that you keep things simple. Use a try block with a catch block(s) or try block with a finally block, but not all three together.
 


















































































   614   615   616   617   618