MSVC - /EHsc vs /EHa (synchronous vs asynchronous exception handling)

后端 未结 2 1451
隐瞒了意图╮
隐瞒了意图╮ 2020-12-29 23:52

Could you give a bullet list of practical differences/implication? I read relevant MSDN article, but my understanding asynchronous exceptions is still a bit hazy.

I

相关标签:
2条回答
  • 2020-12-30 00:08

    When you use /EHsc, the compiler will only emit code for exception filters when it can detect that the code wrapped in the try {} block might throw a C++ exception. An exception filter ensures that the destructor of any local C++ objects get called when the stack is unwound while handling an exception. It makes RAII work.

    That's an optimization, space and time for x86 code, space for x64 code. Space because it can omit the exception filter code, which is modest btw. Time because on x86 it can avoid registering the exception filter upon entering the try {} block. Very modest btw. x64 uses a different way to find exception filters, it is table-based.

    The key phrase in the first paragraph is "might throw a C++ exception". On Windows there are other sources of exceptions. Like the "a" in /EHa, asynchronous exceptions that are raised by the hardware. Things like floating point exceptions, division by zero, and the all-mighty access violation exception. But also notably the kind of exceptions that are raised by code that you might interop with. Like managed code, basically anything that runs in a VM.

    When you want to make your objects safe for these kind of exceptions as well then you'll need to use /EHa, that tells the compiler to always register the exception filter.

    Beware of a nasty side-effect of /EHa, it makes catch(...) swallow all exceptions. Including the ones you should never catch, like AV and SO. Look at __try/__except and _set_se_translator() if that's important to you.

    0 讨论(0)
  • 2020-12-30 00:20

    Well, without /EHa you can't catch unexpected exceptions so your test program will exit abnormally.

    If the test can be made for abnormal exit it'd be better not wrestle with /EHa.

    Moreover, changing option to /EHa incur many side effects including increase in executable size and little performance drop. So effectively changes original behavior.

    To use or not to use /EHa is a big decision usually made at program design stage.

    IMHO, problems at test case writing are rather insignificant to change this decision.

    0 讨论(0)
提交回复
热议问题