I have a stategy working right now (which I think is elegant),
but it doesn't seem to work under Win32. This is what I do:
1) set my own _matherr() function, which intercepts exceptions
generated by the C RTL. In this function, gather some info
into an ErrorInfo object (which I have defined) and throw it
as a C++ exception.
2) with the signal() function, set a handler (which I call
fpthrow()) which will get called if a FPU exception occurs
in my code. Again, gather some info into an ErrorInfo object
and throw it as a C++ exception.
3) now, any block of code that I want to protect, I simply
surround it with a try/catch block.
This works wonderfully in Win16, but I haven't been able to get
it to work at all under Win32. (it causes GPF's sometimes).
Does anyone have a standard methodology or example of how to handle
floating point exceptions?
-----------------------------------------------------------
Daniel G. Hyams, author CurveExpert 1.x
email: dhy...@eng.clemson.edu
phone: (803) 639-4722 (with voicemail)
fax: (803) 639-4722
visit the CurveExpert home page at
http://www.eng.clemson.edu/~dhyams/CurveExpert.html
-----------------------------------------------------------
The IEEE standard defines several encodings for 'NaN' and 'infinity' and
stuff. These might be useful. Except, I've never figured out how to
guarantee that it will give me these things and that it won't raise
exceptions. Can anyone advise?
--
Lucian
> In article <4cjg3b$e...@hubcap.clemson.edu>,
> Daniel Hyams <dhy...@eng.clemson.edu> wrote:
> >I was wondering what was the standard way to handle floating
> >point exceptions in Win16 and Win32. For example, all spreadsheets,
> >when they get a divide-by-zero, simply display 'ERR' in the cell.
> >Surely they don't check every argument that they divide by to see
> >if it is zero! I assume that they incorporate some floating point
> >exception handler of some sort.
> >
>
> Don't use C functions - they don't work in Win32 environment.
> You have to use structured exception handling to catch floating
> exceptions. MSVC allows to use both C++ exception and SEH in CPP files
> (but not ti mix them in one function). BC does not allow to mix them.
>
> Write exception filter to catch them. You can use EXCEPTION_INFO (don't
> remember exactly) to examine all the processor state at the exeption.
>
> Make sure you enabled floating exeptions. Use _controlfp function.
>
Thanks a million. I had tried structured handling, but was
using _defaultfp to initialize the coprocessor registers. Apparantly,
that works in Win16, since the control word is initialized to Ox1332
(if I remember right), which masks denormalization, precision loss,
and the like, but allows the divide-by-zero, overflow, etc., to
propogate.
In Win32, for some reason, _defaultfp initializes it to a value
(which I have not obtained yet, but will) that masks divide-by-
zero, which is the exception I wanted to catch. Using
_control87(0x1332,0xFFFF) to initialize solved the problem.
Again, thanks for the advice!