Some math functions in a program I recently wrote are returning unacceptable values, such as NaN (possibly due to not checking some function’s input params). The problem is that it’s being quite difficult to track down which functions are passing the wrong values. This results in errors being propagated throughout the code and making the program crash minutes or hours later, if at all.
I wonder if there’s a way to catch these faulty operations the moment a NaN value results from any operation (pretty much the same as in the ‘DivisionByZero exception’ thrown by some C/C++ compilers, if I remember).
Thanks in advance.
P.D: Please feel free to re-tag my question if needed.
Without seeing your code this answer is going to be necessarily vague, but one way of doing it is to check the output from your function and if it’s “NaN” raise and exception:
But with more details about the exception.
UPDATE
To trap where a specific exception is being thrown you could (temporarily) break when the exception is thrown in the debugger.
Select Debug > Exceptions then expand the tree to select Common Language Runtime Exceptions > System > System.ArithmeticException and check the “Thrown” option.
The problem with this is that it will break everywhere this is thrown, not just in your code. Putting explicit code at a low enough level gets around this.