CSharpFeeds - All your C# feeds in one place.

Sponsors

Sunday, May 11, 2008

try/catch/finally and resource protection: mistakes to avoid

by Christophe via Christophe on 5/11/2008 6:17:34 PM

I had to take over a project last week, an ASP.NET application. I found a lot of mistakes regarding resources protection and the use of try/catch/finally block. For instance, I found that:

SqlConnection con = new SqlConnection()
try
{
  con.Open();
  // Some code here
}
catch (Exception ex)
{
  throw ex;
}
finally
{
  if (con != null)
    con.close();
}

During a normal processing, if the code inside the try block works, everything is fine. No problem.

Now, imagine:

1- // Some code here raises an exception.
First, the execution goes to the catch block. This one does nothing than re-throwing the same exception (by the way, I advice you to read this post about rethrowing exceptions and preserving the full call stack trace, explaining how you will loose some important information like the stack trace of the exception). In this case, the block is useless.
Then, the execution goes to the finally block. The connection exists (con != null) and is closed. But the test is wrong… we should check if the connection is not null, and not if the connection is opened! The test is quite different, even if it works in this case.

2- con.Open() fails and raises an exception
The execution goes in the catch block (still useless) and then in the finally block. In this case, the connection exists (con != null) and is closed. But the con.Close() call will throw an exception, because the previous con.Open() didn’t work. The current exception will be lost (ie. “con.Open() failed”) and will be replaced by another one (ie. “con.Close() failed”). You loose some information about the problem you had.

This is now a better way to write your code:

SqlConnection con = new SqlConnection()
con.Open();
try
{
  // Some code here
}
finally
{
  con.Close();
}

In this situation:

1- If con.Open() raises an exception
The execution goes out immediately, with the “con.Open() failed” exception.

2- If // Some code here raises an exception
The execution goes directly to the finally block, closes the connection (that was previously opened) and then goes out of the function with the appropriate exception.

This method works fine, and is also much more readable!

Talking about connections, you can also use in a better way the using statement! For instance:

using (SqlConnection con = new SqlConnection())
{
  // Some code here
}

The generated MSIL code will be translated as a try/finally block (as shown in this post about the using statement), just like our previous sample. This one is just easier to read and to write, and you will not forget to close the connection.
email it!bookmark it!digg it!

Original Post: try/catch/finally and resource protection: mistakes to avoid

Subscribe

New Feed

Product Spotlight

Recently Updated Sources

Legal Note

The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.

Advertise with us