问题描述
#include <iostream>
#include <float.h>
#pragma fenv_access (on)
int main(int,char**argv)
{
unsigned int fp_control_word;
_controlfp_s(&fp_control_word,0);
const unsigned int new_fp_control_word = fp_control_word | _EM_INVALID | _EM_DEnorMAL
| _EM_ZERODIVIDE | _EM_OVERFLOW | _EM_UNDERFLOW | _EM_INEXACT;
_controlfp_s(&fp_control_word,new_fp_control_word,_MCW_EM);
try
{ std::cout << std::atof(argv[1]) / std::atof(argv[2]) << std::endl;
} catch (...)
{ std::cout << "caught exception" << std::endl;
}
}
我记得,可以使用try-catch块在Windows上捕获内存访问错误。
关于这个主题已经存在一个问题。但这已经有10年的历史了,所提供的代码不会导致异常,而会导致打印NAN。
我一直对使用此功能以一种很好的方式中止某些数字代码感到好奇。这样做的动机是立即中止一些非常复杂的代码,如果该代码中的任何地方发生了浮点异常,而不是继续使用NAN结果评估其余代码,这很慢,而且毫无意义。>
请:我不在乎C ++标准是否支持!
问题是,如何使此代码运行到catch块中-例如通过使用命令行参数0.0 0.0!
对我来说,它总是打印出NAN。
需要使用哪些编译器选项?
还是需要更改代码?
如果在try块中引起nullptr取消引用,则将在catch块中结束。但不是除以零。 需要使用编译器选项/ EHa来启用结构化异常处理。
解决方法
感谢https://stackoverflow.com/users/17034/hans-passant提供解决方案。
以下是工作代码:
#include <iostream>
#include <float.h>
#pragma fenv_access (on)
int main(int,char**argv)
{
unsigned int fp_control_word;
_controlfp_s(&fp_control_word,_MCW_EM);
const unsigned int new_fp_control_word = fp_control_word & ~(_EM_INVALID
| _EM_DENORMAL | _EM_ZERODIVIDE | _EM_OVERFLOW | _EM_UNDERFLOW | _EM_INEXACT);
_controlfp_s(&fp_control_word,new_fp_control_word,_MCW_EM);
try
{ std::cout << std::atof(argv[1]) / std::atof(argv[2]) << std::endl;
} catch (...)
{ std::cout << "caught exception" << std::endl;
}
}