I am struggling to get a reliable way to catch floating points exceptions under Visual Studio (2005 or 2008). By default, under visual studio, floating point exceptions are not caught, and they are quite hard to catch (mainly because most of them are hardware signal, and need to be translated into exceptions)
Here is what I did :
- Turn on SEH exceptions handling
(properties / code generation / Enable C++ Exceptions : Yes with SEH Exceptions)
- Activate floating points exceptions using _controlfp
I do now catch the exceptions (as shown in the example below which a simple divide by zero exception). However, as soon as I catch this exception, it seems that the program is irremediably corrupted (since simple float initialization, as well as std::cout won't work!).
I have built a simple demo program that shows this rather weird behavior.
Note : this behavior was reproduced on several computers.
#include "stdafx.h"
#include <math.h>
#include <float.h>
#include <iostream>
using namespace std;
//cf http://www.fortran-2000.com/ArnaudRecipes/CompilerTricks.html#x86_FP
//cf also the "Numerical Recipes" book, which gives the same advice
//on how to activate fp exceptions
void TurnOnFloatingExceptions()
{
unsigned int cw;
// Note : same result with controlfp
cw = _control87(0,0) & MCW_EM;
cw &= ~(_EM_INVALID|_EM_ZERODIVIDE|_EM_OVERFLOW);
_control87(cw,MCW_EM);
}
//Simple check to ensure that floating points math are still working
void CheckFloats()
{
try
{
// this simple initialization might break
//after a float exception!
double k = 3.;
std::cout << "CheckFloatingPointStatus ok : k=" << k << std::endl;
}
catch (...)
{
std::cout << " CheckFloatingPointStatus ==> not OK !" << std::endl;
}
}
void TestFloatDivideByZero()
{
CheckFloats();
try
{
double a = 5.;
double b = 0.;
double c = a / b; //float divide by zero
std::cout << "c=" << c << std::endl;
}
// this catch will only by active:
// - if TurnOnFloatingExceptions() is activated
// and
// - if /EHa options is activated
// (<=> properties / code generation / Enable C++ Exceptions : Yes with SEH Exceptions)
catch(...)
{
// Case 1 : if you enable floating points exceptions ((/fp:except)
// (properties / code generation / Enable floting point exceptions)
// the following line will not be displayed to the console!
std::cout <<"Caught unqualified division by zero" << std::endl;
}
//Case 2 : if you do not enable floating points exceptions!
//the following test will fail!
CheckFloats();
}
int _tmain(int argc, _TCHAR* argv[])
{
TurnOnFloatingExceptions();
TestFloatDivideByZero();
std::cout << "Press enter to continue";//Beware, this line will not show to the console if you enable floating points exceptions!
getchar();
}
Does anyone have a clue on what could be done to correct this situation? Many thanks in advance!
You have to clear the FPU exception flags in the status word when you catch a floating point exception. Call _clearfp()
.
Consider using _set_se_translator()
to write an exception filter that translate the hardware exception to a C++ exception. Be sure to be selective, only translate the FPU exceptions.
Additional information: If you are running 32-bit code on 64-bit windows, and use /arch:SSE2
or other options that enable the SSE2 instruction set, or one of its supersets, you may need to do a more drastic reset.
With Visual Studio 2015 (and later versions as far as VS.2022), you need to call _fpreset()
after floating-point traps generated in the SSE2 registers, rather than just _clearfp()
. If you do this with Visual Studio 2013 and earlier, you get a variety of weird problems, caused by the run-time library getting confused.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With