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:

Community
  • 1
  • 1
BCS
  • 75,627
  • 68
  • 187
  • 294
  • Clarify plz: do you want to have the debugger stop on unhandled exceptions but only in certain methods? Or do you want to put a stop inside of your catch block? – jcollum Jan 06 '09 at 23:48
  • @jcollum, nether. I want the debugger to stop at the point of throw but only if the exception is going to be caught at a given location. – BCS Jan 06 '09 at 23:53
  • The location that caused the exception ought to be in your stack trace (not 100% on that but). That's the best you're gonna get. – jcollum Jan 07 '09 at 00:04

2 Answers2

5

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
  • 30,088
  • 6
  • 78
  • 89
  • No that is per exception class (If I'm reading it right) I want anything caught at a given location. – BCS Jan 06 '09 at 23:36
  • 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 Jan 06 '09 at 23:45
2

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
  • 43,623
  • 55
  • 191
  • 321
  • I don't think that works as that will break after the stack has been unwound to that point. :( – BCS Jan 06 '09 at 23:55
  • BCS: putting a break on the "throw"? There's no other way to do what he wants AFAIK. – jcollum Jan 07 '09 at 00:02
  • 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 Jan 07 '09 at 12:16