问题描述
我正在检查 Visual Studio 2013 中的硬件和软件异常.我知道我可以通过将启用 C++ 异常"选项设置为/EHa(是的,带有 SEH 异常)来捕获硬件异常.我正在尝试捕获以下异常:
I'm examining hardware and software exceptions in visual studio 2013. I know that I can catch hardware exceptions by setting 'Enable C++ Exceptions' option to /EHa (Yes with SEH Exceptions). I'm trying to catch the following exceptions:
EXCEPTION_ARRAY_BOUNDS_EXCEEDED - 没抓到
EXCEPTION_ARRAY_BOUNDS_EXCEEDED - didn't catch
EXCEPTION_ACCESS_VIOLATION - 抓到
EXCEPTION_ACCESS_VIOLATION - caught
EXCEPTION_INT_OVERFLOW - 没有捕获
EXCEPTION_INT_OVERFLOW - didn't catch
EXCEPTION_INT_DIVIDE_BY_ZERO - 捕获
EXCEPTION_INT_DIVIDE_BY_ZERO - caught
这是一个代码示例.
try {
a = std::numeric_limits<int>::max();
a += 5;
}
catch (...){
std::cout << "EXCEPTION_INT_OVERFLOW Exception Caught" << std::endl;
exit(1);
}
try {
int h = 0;
b = b / h;
}
catch (...){
std::cout << "EXCEPTION_INT_DIVIDE_BY_ZERO Exception Caught" << std::endl;
exit(1);
}
它只捕获被零除的异常.这取决于处理器,还是其他原因?还有一个小问题,调试版本和发布版本之间有什么区别吗?
It catches only divide by zero exception.Is this dependent of processor, or there is something else?One more little question, is there any difference between debug and release builds?
推荐答案
是的.操作系统仅将硬件陷阱映射到结构化异常,它没有添加逻辑来检测硬件没有检测到的条件.(另一方面,JVM 或 CLR 等托管框架通常会添加逻辑.在软件中捕获这些当然会带来性能损失,而硬件陷阱逻辑是免费的,除非陷阱实际发生.)
Yup. The OS only maps hardware traps to structured exceptions, it doesn't add logic to detect conditions that the hardware doesn't. (On the other hand, managed frameworks such as the JVM or CLR often do add logic. Catching these in software of course carries a performance penalty, while hardware trap logic is free unless the trap actually occurs.)
现在,这并不是说您不能在 x86 处理器上接收 EXCEPTION_INT_OVERFLOW
.但是条件不是您所期望的——在添加过程中仅仅环绕不会导致陷阱.请参阅 Raymond Chen 的博客条目:
Now, this isn't to say that you cannot receive EXCEPTION_INT_OVERFLOW
on x86 processors. But the conditions are not what you expect -- mere wraparound during addition does not cause a trap. See Raymond Chen's blog entry:
- In the red corner,
EXCEPTION_INT_DIVIDE_BY_ZERO
andSTATUS_INTEGER_DIVIDE_BY_ZERO
; and in the blue corner,EXCEPTION_INT_
andSTATUS_INTEGER_OVERFLOW
这篇关于C++ try-catch 块不捕获硬件异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!