Exception Handling Mistakes: Finally Block Does Not Require The Catch Block

While reviewing some code this weekend, I had the thought to do a search for the following string throughout the codebase, "catch(Exception" (using the regular expression search of course it looked more like "catch\s*(\s*Exception").

My intent was to take a look to see how badly Catch(Exception...) was being abused or if it was being used correctly. One interesting pattern I noticed frequently was the following snippet...

try
{
    fs = new FileStream(filename, FileMode.Create);
    //Do Something
}
catch(Exception ex)
{
    throw ex;
}
finally
{
    if(fs != null)
        fs.Close();
}

My guess is that the developer who wrote this didn’t realize that you don’t need a catch block in order to use a finally block. The finally block will ALWAYS fire whether or not there is an exception block. Also, this code is resetting the callstack on the exception as I’ve written about before.

This really just should be.

try
{
    fs = new FileStream(filename, FileMode.Create);
    //Do Something
}
finally
{
    if(fs != null)
        fs.Close();
}

Another common mistake I found is demonstrated by the following code snippet.

try
{
    //Do Something.
}
catch(Exception e)
{
    throw e;
}

This is another example where the author of the code is losing stack trace information. Even worse, there is no reason to even perform a try catch, since all that the developer is doing is rethrowing the exact exception being caught. I ended up removing the try/catch blocks everywhere I found this pattern.

What others have said

Requesting Gravatar... Scott Williams Jan 09, 2006 3:44 AM
# re: Exception Handling Mistakes: Finally Block Does Not Require The Catch Block
Exceptions are silly. I just do a try { /* do stuff */ } catch {} and never get any exceptions any more!





(kidding)
Requesting Gravatar... Haacked Jan 09, 2006 6:07 AM
# re: Exception Handling Mistakes: Finally Block Does Not Require The Catch Block
Wow, I never thought of that! TDD is so tired, Exceptionless programming is so wired! ;)
Requesting Gravatar... Joe Brinkman Jan 09, 2006 6:18 AM
# re: Exception Handling Mistakes: Finally Block Does Not Require The Catch Block
or better yet how about

try{
fs = new FileStream(filename, FileMode.Create);
//Do Something
}
finally
{
try{
fs.Close();
}
catch(Exception e){
throw e;
}
}

Who needs null pointer checks when exceptions are so easy to code for ;)
Requesting Gravatar... Haacked Jan 09, 2006 6:38 AM
# re: Exception Handling Mistakes: Finally Block Does Not Require The Catch Block
Oh geez that hurts my head Joe.
Requesting Gravatar... jayson knight Jan 09, 2006 3:21 PM
# re: Exception Handling Mistakes: Finally Block Does Not Require The Catch Block
Of course you missed the obvious "using" construct as FileStream derives from IDisposable ;-).

Things look a little different around here! Either you rethemed, or subText has been updated. First "complaint" (if you can call it that): The text in this textbox for leaving comments is TINY, I'd say about 6pts.

Lookin' good!
Requesting Gravatar... jayson knight Jan 09, 2006 3:28 PM
# re: Exception Handling Mistakes: Finally Block Does Not Require The Catch Block
Disregard the "using" portion of my post above, reflecting on FileSystem.Dispose shows that it only flushes the stream, and doesn't close it...I should do my homework first.
Requesting Gravatar... Haacked Jan 09, 2006 3:36 PM
# re: Exception Handling Mistakes: Finally Block Does Not Require The Catch Block
Jayson, no you were right the first time. FileStream.Close simply calls FileStream.Dispose.

Therefore the using construct would be appropriate and in my opinion even better here. But I was just making the point about how to use try finally.

I should have probably used an example of a non-disposable type.
Requesting Gravatar... Binu Aug 30, 2006 9:59 PM
# re: Exception Handling Mistakes: Finally Block Does Not Require The Catch Block
What if an Exception occurs in a finally block..Don't say we can write try-catch in finally block...any other proper way..plz share me friends ?
Requesting Gravatar... Abhay Kale Feb 26, 2009 2:06 AM
# re: Exception Handling Mistakes: Finally Block Does Not Require The Catch Block
What if there was an exception within the finally block ? How does one handle that ?
Requesting Gravatar... torzsmokus Nov 23, 2011 5:50 PM
# re: Exception Handling Mistakes: Finally Block Does Not Require The Catch Block
Re Abhay: see the StackOverflow question about exceptions in finally blocks and this article about stream handling in java.
In Java7, there is try-with-resources which is similar to C#’s using.

What do you have to say?

(will show your gravatar)
Please add 3 and 5 and type the answer here: