为什么在启用浮点异常后出现多个陷阱错误 [英] Why after enabling floating point exceptions I got multiple traps error
问题描述
带有MSVC2015专业版的Windows 10环境,使用/EHa编译
Environment Windows 10 with MSVC2015 professional, compile with /EHa
我在做什么:启用浮点异常,以便在发生某些不良情况时能够捕获异常,仅用于调试
What I'm doing: enabling floating point exception to be able to catch exceptions when some bad things happen, just for debuging
代码:
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 ,但有
请帮助了解发生了什么事?预先感谢!
Please help to understand what is going on? Thanks in advance!
推荐答案
我在MSDN上找到了答案,x86平台存在问题,在x64上,我们可以从线程上下文寄存器中获取正确的异常代码,请参见ref-//
I found answer on MSDN, problem with x86 platform, on x64 we can get correct exception code from thread context register see ref - // https://social.msdn.microsoft.com/Forums/en-US/48f63378-19be-413f-88a5-0f24aa72d3c8/the-exceptions-statusfloatmultipletraps-and-statusfloatmultiplefaults-is-needed-in-more
这篇关于为什么在启用浮点异常后出现多个陷阱错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!