使用MSVC2015专业环境Windows 10,使用/ EHa编译
我在做什么:启用浮点异常,以便在发生一些不良事件时捕获异常,仅用于调试
代码:
namespace debug_details
{
void (*defaultStructuredExceptionFunc)(unsigned int, PEXCEPTION_POINTERS) = nullptr;
void my_trans_func(unsigned int exceptionCode, PEXCEPTION_POINTERS pExpInfo)
{
switch (exceptionCode)
{
case EXCEPTION_FLT_DENORMAL_OPERAND:
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
case EXCEPTION_FLT_INEXACT_RESULT:
case EXCEPTION_FLT_INVALID_OPERATION:
case EXCEPTION_FLT_OVERFLOW:
case EXCEPTION_FLT_STACK_CHECK:
case EXCEPTION_FLT_UNDERFLOW:
{
_clearfp();
std::stringstream ss;
ss << "floating-point structured exception: 0x" << std::hex << exceptionCode;
throw std::runtime_error(ss.str());
}
default:
if (defaultStructuredExceptionFunc != nullptr)
{
defaultStructuredExceptionFunc(exceptionCode, pExpInfo);
}
};
};
void EnableFloatingPointExceptions()
{
unsigned int fe_value = ~(/*_EM_INVALID | _EM_DENORMAL |*/ _EM_ZERODIVIDE | _EM_OVERFLOW | _EM_UNDERFLOW /* | _EM_INEXACT*/);
unsigned int mask = _MCW_EM;
unsigned int currentWord = 0;
_clearfp();
errno_t result = _controlfp_s(¤tWord, fe_value, mask); // https://msdn.microsoft.com/en-us/library/c9676k6h.aspx
DVASSERT(result == 0);
debug_details::defaultStructuredExceptionFunc = _set_se_translator(&debug_details::my_trans_func); // https://msdn.microsoft.com/en-us/library/5z4bw5h5.aspx
float32 div = 0.f;
float32 f = 15.f / div;
float32 f2 = 30.f * div;
}
} // end namespace debug_details
我希望 EXCEPTION_FLT_DIVIDE_BY_ZERO ,但有
请帮助了解发生了什么?提前谢谢!
答案 0 :(得分:0)
我在MSDN上找到答案,x86平台问题,在x64上我们可以从线程上下文寄存器中获取正确的异常代码,参见ref - // https://social.msdn.microsoft.com/Forums/en-US/48f63378-19be-413f-88a5-0f24aa72d3c8/the-exceptions-statusfloatmultipletraps-and-statusfloatmultiplefaults-is-needed-in-more
答案 1 :(得分:-1)
关于FP例外,有几点需要了解: