Re: Handling Floating Point Exception in VC++ 6.0
On Thu, 4 Dec 2008 13:41:33 -0600, "John Keenan"
<john.removeme.keenan@optimapowerware.com> wrote:
throw( new FloatException( nSubCode ) );
catch(FloatException& e)
You are throwing a pointer but catching a reference. Try changing the throw
to:
throw( FloatException( nSubCode ) );
Good catch, I didn't notice that. I will be interested to hear if that was
the problem, because it implies that using a signal handler works. Even if
it does, I think the preferred way remains to use _set_se_translator. The
signal documentation says, in part:
<q>
To recover from floating-point exceptions, use try/except clauses
surrounding the floating point operations. It is also possible to recover
using setjmp with longjmp. In either case, the calling process resumes
execution with the floating-point state of the process left undefined.
</q>
Then there are a bunch of restrictions on what a signal handler can do. I
did not see any mention of C++ exceptions at all in the topic. Concerning
the part I quoted, I think it's describing two options, (1) use
__try/__except around the FP operations, i.e. use SEH, in which case,
_set_se_translator is the way to get C++ exceptions out of it, or (2) use
setjmp/longjmp, with the longjmp being in the signal handler. Using
signal() is not part of (1), and setjmp/longjmp pretty much have no place
in a C++ program.
Also, the OP was expecting exp(99999999) to cause an FP exception. Unless
it's documented to do so when FP exceptions are enabled, I wouldn't expect
that behavior, as it is defined to return an error value and set errno when
an error occurs. ISTR there is a function _matherr that may be helpful
here, but I haven't used that function since Microsoft C 5. That's C, not
C++, so it's been a while. :)
--
Doug Harrison
Visual C++ MVP