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
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:


":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:

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


About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Google Reader or Homepage Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of
Red-Gate Tools For SQL and .NET


SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
NHibernate Profiler
Balsamiq Mockups
JetBrains - ReSharper
Web Sequence Diagrams
Ducksboard<-- NEW Friend!


Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers


Community Server (Commercial Edition)