The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Essential Java Classes
Lesson: Handling Errors with Exceptions

Putting It All Together

The previous sections describe how to construct the try, catch, and finally code blocks for the writeList example. 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:

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());
         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");
        }
    }
}  
As mentioned previously, the try block in this method has three different exit possibilities.
  1. The new FileWriter statement fails and throws an IOException.
  2. The victor.elementAt(i) statement fails and throws an ArrayIndexOutOfBoundsException.
  3. Everything succeeds and the try statement exits normally.
Letís look at what happens in the writeList method during the two of these exit possibilities.

Scenario 1: An IOException Occurs

The statement that creates a PrintWriter can fail for a number of reasons. For example, the constructor for PrintWriter throws an FileNotFoundException if 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 PrintWriter throws a FileNotFoundException, the runtime system immediately stops executing the try block. The runtime system then starts searching at the top of the method call stack for an appropriate exception handler. In this example, when the FileNotFoundException occurs, the PrintWriter constructor is at the top of the call stack. However, the PrintWriter constructor doesnít have an appropriate exception handler, so the runtime system checks the next method in the method call stack—the writeList method. The writeList method has two exception handlers: one for FileNotFoundException and one for IOException.

The runtime system checks writeListís handlers in the order in which they appear after the try statement. The argument to the first exception handler is FileNotFoundException. 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 that catch clause is executed.

After the exception handler has executed, the runtime system passes control to the finally block. In this scenario, the PrintWriter was never opened and doesnít need to be closed. After the finally block has completed executing, the program continues with the first statement after the finally block.

Hereís the complete output that you see from the ListOfNumbers program when a FileNotFoundException is thrown:

[** verify this is correct output 
Entering try statement
Caught IOException: OutFile.txt
PrintWriter not open ***] 
The boldface code in the following listing shows the statements that get executed during this scenario:
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");
        }
    }
}  

Scenario 2: The try Block Exits Normally

In this scenario, all the statements within the scope of the try block execute successfully and throw no exceptions. Execution falls off the end of the try block, and the runtime system passes control to the finally block. Because everything was successful, the PrintWriter is open when control reaches the finally block, which closes the PrintWriter. Again, after the finally block has completed executing, the program continues with the first statement after the finally block.

Here is the output from the ListOfNumbers program when no exceptions are thrown:

Entering try statement
Closing PrintWriter
The boldface code in the following code sample shows the statements that get executed during this scenario:
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");
        }
    }
}  

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.