tags:

views:

527

answers:

4

I am reviewing some code.

I have notice some empty catch blocks. Not a good idea since somethings do not work and you cannot see why.

Is there an easy way to find all empty try catch blocks in a solution?

EDIT

Thanks for all the answers.

Used the example given by Stefan. Had to do a few variations, there are many ways of writing empty catch blocks. Also a simple search for catch (Exception) found several errors.

+5  A: 

Do you have ReSharper? This should hilight the issues found in code.

astander
I haven't found where or how to this in ReSharper... how would you do it?
yeyeyerman
It shows up as a warning with ReSharper, but I think you have to have the code file open to see it.
jrummell
Oddly enough, I found one of these in some code the other day. We use both FxCop and ReSharper; someone had explicitly suppressed the FxCop warning (so it didn't show up on the Continuous Integration build) and ignored the ReSharper one. Not much you can do about that. :P
TrueWill
+7  A: 

FxCop will find them along with many other potential issues.

jrummell
+15  A: 

Use use the global find dialog, turn on regular expressions and then search for:

catch:b*\([^)]*\):b*\{:b*\}
Stefan
Thanks, that is a real time saver
Shiraz Bhaiji
FxCop or ReSharper would be overkill for just empty try/catch, good thinking!
jrummell
A: 

Here is a regular expression that finds also catch blocks with only comments inside : catch:b*([^)])[:b\n]{([:b\n]|(\/*[^]*\/)|(//[^\n]))}

Toto