启用浮点异常后的Visual C++ /奇怪行为(编译器错误?)

Pas*_* T. 8 floating-point exception divide-by-zero visual-c++

我正在努力获得一种可靠的方法来捕获Visual Studio(2005或2008)下的浮点异常.默认情况下,在visual studio下,浮点异常没有被捕获,并且它们很难捕获(主要是因为它们中的大多数是硬件信号,需要转换为异常)

这是我做的:
- 打开SEH异常处理
(属性/代码生成/启用C++异常:是的SEH异常)
- 使用_controlfp激活浮点异常

我现在抓住异常(如下面的例子中所示,简单的除零除外).但是,一旦我捕获到这个异常,似乎程序被无可挽回地破坏了(因为简单的float初始化,以及std :: cout将不起作用!).

我已经构建了一个简单的演示程序,显示了这种相当奇怪的行为.

注意:此行为已在多台计算机上重现.

#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();
}
Run Code Online (Sandbox Code Playgroud)

有没有人知道如何纠正这种情况?提前谢谢了!

Han*_*ant 10

捕获浮点异常时,必须清除状态字中的FPU异常标志.调用_clearfp().

考虑使用_set_se_translator()编写一个异常过滤器,将硬件异常转换为C++异常.一定要有选择性,只能翻译FPU例外.

  • 但重要的一点是:_fpreset()将清除浮点状态字*并*重新初始化浮点数学包,即不会随后抛出异常.为了不禁用后续异常,可以使用_clearfp()代替 (2认同)