views:

568

answers:

8

I'm debugging a production application that has a rash of empty catch blocks sigh:

try {*SOME CODE*}
catch{}

Is there a way of seeing what the exception is when the debugger hits the catch in the IDE?

A: 

Can't you just add an Exception at that point and inspect it?

John Nolan
+3  A: 

In Visual Studio - Debug -> Exceptions -> Check the box by "Common Language Runtime Exceptions" in the Thrown Column

Mike Schall
+3  A: 

Hi Rik,

In VS, if you look in the Locals area of your IDE while inside the catch block, you will have something to the effect of $EXCEPTION which will have all of the information for the exception that was just caught.

AdamB
I couldn't see that anywhere
Rik Garner
Where have you seen that? Extend the information
SoMoS
+1  A: 

You can write

catch (Exception ex) { }

Then when an exception is thrown and caught here, you can inspect ex.

sectrean
+1  A: 

No it is impossible, because that code block says "I don't care about the exception". You could do a global find and replace with the following code to see the exception.

catch {}

with the following

catch (Exception exc) {
#IF DEBUG
    object o = exc;
#ENDIF
}

What this will do is keep your current do nothing catch for Production code, but when running in DEBUG it will allow you to set break points on object o.

Nick Berardi
A: 

Sorry should have been clearer - I can't alter the code - I'm just trying to track down some bugs

Rik Garner
A: 

If you're using Visual Studio, there's the option to break whenever an exception is thrown, regardless of whether it's unhandled or not. When the exception is thrown, the exception helper (maybe only VS 2005 and later) will tell you what kind of exception it is.

Hit Ctrl+Alt+E to bring up the exception options dialog and turn this on.

Chris Karcher
This is the mechanism I ended up using - I couldn't find a way of seeing the hidden excecption as AdamB described
Rik Garner
A: 

@sectrean

That doesn't work because the compiler ignores the Exception ex value if there is nothing using it.

Nick Berardi