views:

344

answers:

2

In the VS debugger, un-caught exceptions result in the program breaking at the point the exception is throw (or near enough) and in a state that lets you look at all the stack frames and there local variables up to that point.

Is there a way to get this same result (break at throw) but for an exception is caught at a particular point? I'm not interested in doing this for ALL exceptions or even all exception of a given type (that could get useless real quick) but if I could do it for a single try or catch block I'd be happy

somewhat related:

+4  A: 

Does the Debug -> Exceptions dialog do what you want? You can select which Exceptions that will cause VS to break, regardless of whether they are caught or not. I don't know of a way to do this for only a certain part of the code, only based on the type of the exception thrown.

Andy
No that is per exception class (If I'm reading it right) I want anything caught at a given location.
BCS
+1 anyway because It's nice to know about
BCS
Sadly, I don't know of any way to break when anything is thrown at a certain location. I could definitely see that coming in handy, though.
Andy
+2  A: 

Yes, you should be able to put a breakpoint on the last brace of your catch block. Or the throw command if you're re-throwing.

If you just need to have a breakpoint on any exception inside of a certain method do a re-throw.

try {  }
catch (Exception exc)
{ 
   throw;  // <-- breakpoint here
}

Edit: I was once in the habit of putting breakpoints on just about all of my exceptions. Found out the hard way that this slowed down the debugger in a big way once I got to about 25 breakpoints. May only be relevant to VS2005.

Edit2: The location that caused the exception ought to be in the exc object's StackTrace.

jcollum
Also relevant to VS2008
David Norman
I don't think that works as that will break after the stack has been unwound to that point. :(
BCS
BCS: putting a break on the "throw"? There's no other way to do what he wants AFAIK.
jcollum
If you know where the exception is going to be thrown, just put a try/catch block around that part of the code, and the ndo what jcollum says in his answer. Would that work?
Andy