Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Christopher Bennage

Our WPF book is now available!


Discovering Empty Try/Catch Blocks

I've been burned by a few bugs recently due to swallowed exceptions.  In case you don't know, empty catch blocks will hurt you. Really, they will.

I wanted a way to locate all of the empty blocks in our code base, I was playing around with NDepend and the amazing Code Query Language, but it's not able to do something like this yet.  (Patrick said that it will in the next version!)

Then I realized that I was missing the simple solution. The Find and Replace dialog in Visual Studio.Ctrl+F and a regular expression!

Here is the regex you'll need to find empty catch blocks in your solution:

catch:Wh*\{:Wh*\}

":Wh" designates whitespace

* means zero or more

You have to escape the {} as they are special characters.

Update: Note Dan's comments below. Here's an updated regex to cast a wider net:
catch(\(.*Exception.*\))*:Wh*\{:Wh*\}


Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# February 21, 2008 12:05 PM

Derik Whittaker said:

You rock, this is cool
# February 21, 2008 2:34 PM

dan the wonderhorse said:

Nice but no silver bullet. This will never match code which is still surrounded by a catch block, but is merely commented out. not to mention the ubiquitous catch (Exception ex) { }
# February 21, 2008 2:46 PM

Christopher Bennage said:

@Dan,

Excellent points, here is a revised version that will catch the parameter (no matter what the name).  I think I could write one to catch the comments too, but I need to get back to, ahem, work.

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

# February 21, 2008 3:21 PM

Discovering Empty Try/Catch Blocks - Christopher Bennage « Noocyte’s Weblog said:

Pingback from  Discovering Empty Try/Catch Blocks - Christopher Bennage « Noocyte’s Weblog

# February 22, 2008 5:56 AM

Christopher Steen said:

Link Listing - February 21, 2008

# February 22, 2008 8:42 AM

Christopher Steen said:

ASP.NET Embedding ASP.NET Server Variables in Client JavaScript, Part 2 [Via: Rick Strahl ] WPF Major...

# February 22, 2008 8:42 AM

Daily Bits - February 22, 2008 | Alvin Ashcraft's Daily Geek Bits said:

Pingback from  Daily Bits - February 22, 2008 | Alvin Ashcraft's Daily Geek Bits

# February 22, 2008 9:14 AM

Fabrice said:

Looks like something I did before. See http://weblogs.asp.net/fmarguerie/archive/2007/12/23/hunting-down-bad-try-catch-blocks.aspx
# March 22, 2008 11:52 AM

Christopher Bennage said:

@Fabrice I guess I need to subscribe to your feed! BTW, I've heard great things about the book.  It's on my "buy soon" list.

# March 22, 2008 2:18 PM

Fabrice said:

I hope you'll like LINQ in Action. Thanks for the kick on Rick Strahl's review :-)
# March 22, 2008 2:47 PM

Purchase valium. said:

Valium without prescription. Order valium online. Valium toxicity canine. Valium overnight. Side effects of valium. Valium appearance. Valium.

# July 23, 2008 1:05 PM

About Christopher Bennage

Christopher is a software developer and consultant at Blue Spire Consulting, a company he co-founded with Rob Eisenberg in 2006. His interests include programming, liberal education, truth, beauty, and number of deceased British authors (C. S. Lewis, G. K. Chesterton, and most recently Owen Barfield.) He lives in Tallahassee, FL with his wife and three children and still prefers to play as the Night Elves in WarCraft 3. Check out Devlicio.us!

Red-Gate!