ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  Best Practices for Exception Handling
Subject:   Correction of the try/finally code
Date:   2006-11-16 15:34:08
From:   rillig
In the article, there is the following code:



Connection conn = null;
try {
conn = getConnection();
... some code that throws SQLException
} finally {
DBUtil.closeConnection(conn);
}


This code is wrong because it could call closeConnection() with a null pointer. The right way to do this is:



final Connection conn = getConnection();
try {
... some code that throws SQLException
} finally {
DBUtil.closeConnection(conn);
}


It's not only shorter, it also takes care that the conn variable does not get overwritten.


Roland


1 to 1 of 1
  1. Correction of the try/finally code
    2008-01-21 01:00:38  venkataramanam [View]

1 to 1 of 1