views:

58

answers:

2

I am using a thirdparty library eg. Lib::ValueType value. I then do a call to a member function, value.theta() which performs some mathematical operations including a call to atan2 from <cmath>. Sometimes the theta component is empty and an "atan2: domain error" is thrown. However, I can't catch the exception even by wrapping try{}catch(...) around the suspect code.

I am using C++ Builder 2009, any idea as to how the exception is being thrown and not being caught by the IDE, or my code. The error pops straight up to the screen as a dialog. I have selected all the options in the IDE to handle everytype of exception.

+1  A: 

The C standard library isn't aware of C++ exception handling, so try-catch won't work. You might want to look at the matherr function - according to the documentation, you can redefine this function in your program in order to handle math exceptions by yourself.

casablanca
where should the function be defined? as the error is occurring in a thirdparty library
Seth
That's not a standard function. According to your link, it's defined by one particular Unix platform, but it's not mentioned in either the C or C++ standards.
Mike Seymour
@Mike - how do you suggest I catch this?
Seth
@Mike: It's not part of the C standard but it seems to be supported by most implementations, including Linux and Visual C++.@Seth: If the third-party library is statically linked, then simply defining a new `matherr` anywhere in your own program should work. If it's a DLL, I'm not sure there's a straightforward solution. (except as Mark suggests, to verify your input beforehand)
casablanca
@casablanca - yeah that didn't work
Seth
Marking as accepted answer as the first sentence answers the bulk of my question.
Seth
A: 

Unfortunately the C math library doesn't know about C++ exceptions. Most likely you're seeing an unhandled floating point exception from your hardware. atan2 is extremely forgiving about its inputs: The only invalid case is (0, 0) so all you have to do is verify that one argument is nonzero before making the function call to prevent the exception.

EDIT: Then you need to prevent the invalid theta component when calling the function. What does the third party library documentation say about when it's valid to call theta?

Mark B
@Mark - thanks for the answer, I would do that but the problem is I don't have access to atan2's inputs..
Seth
Then how are you calling it?
Duracell
I'm not calling atan2, the function I am calling is calling atan2.
Seth