C ++ try-catch块未捕获硬件异常 [英] C++ try-catch block doesn't catch hardware exception

查看:120
本文介绍了C ++ try-catch块未捕获硬件异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Visual Studio 2013中检查硬件和软件异常.我知道我可以通过将'Enable C ++ Exceptions'选项设置为/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?

推荐答案

这是否取决于处理器

Is this dependent of processor

是的.该操作系统仅将硬件陷阱映射到结构化异常,它没有添加逻辑来检测硬件没有的条件. (另一方面,诸如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 and STATUS_INTEGER_DIVIDE_BY_ZERO; and in the blue corner, EXCEPTION_INT_and STATUS_INTEGER_OVERFLOW

这篇关于C ++ try-catch块未捕获硬件异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆