views:

768

answers:

3

I'm using C# with the XNA library and I'm getting NaNs cropping up in my Vector3 objects. Is there a way to break into the debugger when the offending calculation happens (e.g. a divide by zero)? Currently the program just continues running. I'm using VS2008 Professional. All the exceptions in the Exceptions dialog are selected in the "user-unhandled" column.

Edit: To clarify, I can't work out where the bad calculation is. This is why I want the debugger to break automatically. Setting breakpoints is not a solution.

+3  A: 

Sounds like you're handling the exception somehow (like a catching a generic Exception) What you can do is press Ctrl+alt+E to bring up the exceptions dialog -- make sure you check the "when thrown" checkbox for the exception(s) you're interested in

Rowland Shaw
No different. It just keeps running.
Martin Stone
In that case -- what makes you think the exception is being thrown? (my default is to stop on throwing of any managed exceptions to cover any eventualities). Conditional breakpoints could help as well
Rowland Shaw
Well maybe it's not being thrown. I wish it was. Since the calculation is almost certainly within the XNA library's Vector3 member functions I don't have control over whether it lets exceptions out. I'd expect "when thrown" to work there, though.
Martin Stone
+2  A: 

You could set a conditional breakpoint that only breaks when the divisor of an expression is 0.

Andrew Hare
+2  A: 

Firstly dividing a double/float by zero gives Infinity/-Infinity depending upon whether the double is positive or negative. Only a zero double/float divided by zero gives NaN. In either case, no exception will be thrown.

You should be able to use conditional breakpoints to detect when a particular variable gets set to one of these values. Be careful when checking for NaN though, as NaN != NaN.

double a = double.NaN;
Console.Out.WriteLine(a == double.NaN); // false
Console.Out.WriteLine(a == a); // false
Console.Out.WriteLine(double.IsNaN(a)); // true
Paul Ruane
As far as I can see, conditional breakpoints are evaluated at a particular source line. Is that right? The trouble is I have no idea where the NaN is coming from, which is why I'd just like the debugger to stop when it happens.
Martin Stone
Yes, you have to set the breakpoint on a line of code. However, you should be able to hone in on the source by adding breakpoints and rerunning the code.Initially I thought the 'checked' keyword would help but in my tests it does not.
Paul Ruane
Yes, it appears that the IEEE floating point spec. mandates no exceptions for this situation. Looks like I'll have to find it the hard way. Thanks.
Martin Stone