The finally Block


The finally Block

When an exception is thrown, the Java VM immediately transfers control from its point of origin to the first catch block matching the exception type. Java executes no further lines of code within the TRy block. If no try block exists, the VM immediately transfers control out of the method. You may have the need, however, to ensure that some bit of code is executed before control transfers out.

The optional finally block ensures that the Java VM always executes some piece of code, regardless of whether or not an exception was thrown. You may attach a single finally block to the end of a try-catch block.

The prototypical reason for using a finally block is to clean up any local resources. If you have opened a file, the finally block can ensure that the file gets properly closed. If you have obtained a connection to a database, the finally block ensures that the connection is closed.

 public static Student findByLastName(String lastName)       throws RuntimeException {    java.sql.Connection dbConnection = null;    try {       dbConnection = getConnection();       return lookup(dbConnection, lastName);    }    catch (java.sql.SQLException e) {       throw new RuntimeException(e.getMessage());    }    finally {       close(dbConnection);    } } 

(Note that the example is here for demonstration purposes only. Refer to Additional Lesson III for a brief overview of interacting with databases via JDBC.)

Both the lookup method and the getConnection method can throw an SQL-Exception object. You call the the lookup method after obtaining a connection. If the lookup method subsequently throws an exception, you want to ensure that the database connection gets closed.

If no exception is thrown, Java transfers control to the finally block upon completion of the code in the TRy block. In the example, the finally block makes a call to close the database connection. If an exception is thrown, Java executes code in the catch block. As soon as either the catch block completes or the VM directs control out of the catch block (in the above example, through use of the tHRow statement), Java executes the finally block, which closes the connection.

If you supply a finally block, the catch block itself is optional. For example, you may not want to attempt to handle an SQL exception from within findByLastName. Instead, you declare that findByLastName can throw an SQLException. But you still need to close the connection before the VM transfers control out of findByLastName:

 public static Student findByLastName(String lastName)       throws java.sql.SQLException {    java.sql.Connection dbConnection = null;    try {       dbConnection = getConnection();       return lookup(dbConnection, lastName);    }    finally {       close(dbConnection);    } } 

Under no circumstances should you include a return statement within a finally blockit will "eat" any exceptions that might have been thrown by the catch block. Some developers also promote the more strict notion that you should not return from either a try block or a catch block; instead only return after completion of the whole TRy-catch block.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net