Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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*\}

Posted 02-21-2008 12:03 PM by Christopher Bennage

[Advertisement]

Comments

DotNetKicks.com wrote Discovering Empty Try/Catch Blocks
on 02-21-2008 12:05 PM

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

Derik Whittaker wrote re: Discovering Empty Try/Catch Blocks
on 02-21-2008 2:34 PM
You rock, this is cool
dan the wonderhorse wrote re: Discovering Empty Try/Catch Blocks
on 02-21-2008 2:46 PM
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) { }
Christopher Bennage wrote re: Discovering Empty Try/Catch Blocks
on 02-21-2008 3:21 PM

@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*\}

Discovering Empty Try/Catch Blocks - Christopher Bennage « Noocyte’s Weblog wrote Discovering Empty Try/Catch Blocks - Christopher Bennage « Noocyte’s Weblog
on 02-22-2008 5:56 AM

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

Christopher Steen wrote Link Listing - February 21, 2008
on 02-22-2008 8:42 AM

Link Listing - February 21, 2008

Christopher Steen wrote Link Listing - February 21, 2008
on 02-22-2008 8:42 AM

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

Daily Bits - February 22, 2008 | Alvin Ashcraft's Daily Geek Bits wrote Daily Bits - February 22, 2008 | Alvin Ashcraft's Daily Geek Bits
on 02-22-2008 9:14 AM

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

Fabrice wrote re: Discovering Empty Try/Catch Blocks
on 03-22-2008 11:52 AM
Looks like something I did before. See http://weblogs.asp.net/fmarguerie/archive/2007/12/23/hunting-down-bad-try-catch-blocks.aspx
Christopher Bennage wrote re: Discovering Empty Try/Catch Blocks
on 03-22-2008 2:18 PM

@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.

Fabrice wrote re: Discovering Empty Try/Catch Blocks
on 03-22-2008 2:47 PM
I hope you'll like LINQ in Action. Thanks for the kick on Rick Strahl's review :-)
http://devlicious.com/blogs/christopher_bennage/archive/2008/02/21/discovering-empty-try-catch-blocks.aspx wrote http://devlicious.com/blogs/christopher_bennage/archive/2008/02/21/discovering-empty-try-catch-blocks.aspx
on 04-05-2008 9:52 AM
Tim Barcz wrote Regular Expressions, Your Job Requires Them
on 05-25-2009 10:22 PM

In preparing my talk on Regular Expressions that I'll be giving this upcoming weekend at the Chicago

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)