Wrapping it up in finally
What happens if you absolutely, positively have to do something regardless of whether or not an exception is thrown? You can use a third block for that purpose called the finally block, which can be used only in the try/catch block. The finally block is guaranteed to always execute regardless of whether or not an exception was thrown.
To see how you might use the finally block, suppose that you're performing some database operation in the try block that throws a SQLException. Before making the database call, you get a database connection. You want to release the connection under all circumstances; otherwise it is a nonrecover-able resource. Listing 8-1 shows some code that doesn't guarantee that the connection will always be released.
Listing 8-1 A try/catch Block in Need of a finally Block
1 public Map getCompanies() throws ModuleException
3 HashMap companies = null;
4 Connection conn = null;
5 try
7 conn = dbConnMgr.getConnection();
8 DBFactory dbf = DBFactory.getDBFactory();
9 DBUtility dbu = dbf.getDBUtility();
10 companies = dbu.getCompanies(conn); // retrieve companies from database
13 catch (SQLException se)
15 ModuleException me = new ModuleException("error.company.select");
16 throw me;
18 return companies;
You do not have to understand the details of this code, just the fact that in the try block (lines 5-12), we are getting a database connection and then performing some operations that could throw a SQLException. Here's the problem — if the code in lines 7-10 throws the exception, the release of the connection in line 11 would not take place. The connection would be left in limbo, so to speak.
To circumvent this situation, you can add a finally block to the code, which is guaranteed to always execute no matter what happens (unless the program exits). The code in Listing 8-2 has the finally block added.
Listing 8-2 A try/catch Block Using a finally Block
1 public Map getCompanies() throws ModuleException
3 HashMap companies = null;
4 Connection conn = null;
5 try
7 conn = dbConnMgr.getConnection();
8 DBFactory dbf = DBFactory.getDBFactory();
9 DBUtility dbu = dbf.getDBUtility();
10 companies = dbu.getCompanies(conn); // retrieve companies from database
12 catch (SQLException se)
14 ModuleException me = new ModuleException("error.company.select");
15 throw me;
17 finally
19 try
23 catch(SQLException se)
25 log.error("Could not close the connection. " + se.getMessage());
28 return companies;
We moved the line of code that releases the connection from the try block to the finally block (line 21). This example is a more complex than usual because executing line 21 (conn.close) could also result in a SQLException being thrown. Therefore, we have to wrap the line in a try/catch block also. Generally, statements in the finally block do not cause exceptions.
This review is not intended to be a comprehensive look at how to deal with exceptions in Java. For more detail information on Java Exceptions visit the Java Tutorial at java.sun.com/docs/books/tuton'al/essential/exceptions/index.html
Another good article on exceptions is at
Post a comment