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]

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
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)