Fabrice's weblog

Tools and Source

News

My .NET Toolbox
An error occured. See the script errors signaled by your web browser.
No tools selected yet
.NET tools by SharpToolbox.com

Read sample chapters or buy LINQ in Action now!
Our LINQ book is also available on AMAZON

.NET jobs

Emplois .NET

Tuneo

ASP.NET Hosting transatlantys

Contact

Me

Others

Selected content

Archives

Hunting down bad try..catch blocks

Way too often developers take the easy solution to use try..catch blocks to silence and ignore exceptions.

There are two things to do when you see this happening:

  1. Explain the developers why it's a bad practice (exceptions are costly and their use should be reserved to exceptional cases), and teach them how to do better (use TryParse, test upfront for known problematic values, etc.)
  2. Hunt down the bad blocks and remove them whenever possible

To spot try..catch blocks with empty catch statements, you can use something simple: Visual Studio's search feature, which allows you to search in files. Of course, you can start by looking for catch{} and catch {} and catch { } and catch {}, but that makes several cases, and doesn't cover the cases where there are line breaks. Luckily, Visual Studio's search support regular expressions, so you can devise one that would match all cases. To get you started, here are some you can use catch.*:b*\n:b*\{:b*\n*:b*\} and catch.*:b*\n:b*\{\} and catch.*:b*\n:b*\{.*\}

I wrote these quickly, so they can be improved. Any expert in regular expression should be able to write a single expression that matches all cases... This could be further improved to include the catch blocks that contain comments such as // ignore exceptions or /* simply ignore this problem */.

Comments

Joe said:

You could also simply run FxCop ...

# December 23, 2007 3:39 AM

vikram said:

The easier would be to find the keyword try. As any catch block will have to after a try block find try block (without any need of Regex).

# December 23, 2007 11:25 PM

Fabrice Marguerie said:

Could you explain more precisely what you mean, Vikram?

Not all uses of "try" mean there is a bad use of "catch". It can be part of try..finally or a good try..catch block.

# December 24, 2007 7:20 AM

GoDaddy said:

Is there some good article somewhere that explains when to throw and when not to throw?

# December 24, 2007 2:19 PM

Fabrice Marguerie said:

There are some hints about good practices with exception s in the excellent book "Framework Design Guidelines : Conventions, Idioms, and Patterns for Reusable .NET Libraries" and Powerpoint presentation

"The Art of Building a Reusable Class Library" from Brad Abrams and Krzysztof Cwalina.

See weblogs.asp.net/.../425780.aspx for more information.

# December 24, 2007 7:58 PM

Matt said:

Here is one which will match Catch blocks that contain only comments:

catch(\(.*Exception.*\))*:Wh*\{:Wh*(^:Wh*//.*)*:Wh*\}

# December 15, 2008 1:36 AM

Matt said:

ahh apologies! I forgot that some catch declarations may have spaces inbetween "catch" and the opening "(". The amended regex is "catch:Wh*(\(.*Exception.*\))*:Wh*\{:Wh*(^:Wh*//.*)*:Wh*\}"

# December 15, 2008 1:41 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)