Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

windows - C++ try-catch block doesn't catch hardware exception

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 - didn't catch

EXCEPTION_ACCESS_VIOLATION - caught

EXCEPTION_INT_OVERFLOW - didn't catch

EXCEPTION_INT_DIVIDE_BY_ZERO - caught

This is an example of code.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Is this dependent of processor

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.)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...