Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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

You can write

catch (Exception ex) { }

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

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.

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.

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

@sectrean

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top