MSVC-/EHsc与/EHa(同步与异步异常处理) [英] MSVC - /EHsc vs /EHa (synchronous vs asynchronous exception handling)

查看:455
本文介绍了MSVC-/EHsc与/EHa(同步与异步异常处理)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能否列举一些实际的差异/含义?我阅读了有关MSDN的文章,但是我对异步异常的理解仍然有些朦胧.

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.

我正在使用Boost.Test编写测试套件,并且我的编译器发出警告,要求启用EHa:

I am writing a test suite using Boost.Test and my compiler emits a warning that EHa should be enabled:

警告C4535:调用_set_se_translator()需要/EHa

项目本身仅使用普通异常(来自STL),并且不需要/EHa开关.我是否必须使用/EHa开关重新编译它才能使测试套件正常工作?我的感觉是我只需要/EHa作为测试服.

The project itself uses only plain exceptions (from STL) and doesn't need /EHa switch. Do I have to recompile it with /EHa switch to make the test suite work properly? My feeling is that I need /EHa for the test suit only.

推荐答案

使用/EHsc时,编译器仅能检测到try {}块中包装的代码可能抛出C ++时,才为异常过滤器发出代码.例外.异常过滤器确保在处理异常时取消堆栈堆栈时,将调用任何本地C ++对象的析构函数.它使RAII起作用.

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.

这是x86代码的优化,空间和时间,是x64代码的空间.空格,因为它可以省略异常过滤器代码,该代码为适中的btw.时间,因为在x86上,它可以避免在进入try {}块时注册异常过滤器.顺便说一句. x64使用基于表的其他方法来查找异常过滤器.

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.

第一段中的关键词是可能抛出C ++异常".在Windows上,还有其他异常来源.像/EHa中的"a"一样,硬件引发的异步异常.诸如浮点异常,被零除和万能的访问冲突异常之类的东西.但也要特别注意的是,您可能会互操作的代码引发的异常类型.像托管代码一样,基本上任何可以在VM中运行的东西.

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.

当您还希望使对象对于此类异常安全时,则需要使用/EHa,这告诉编译器始终 注册异常过滤器.

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.

当心/EHa的令人讨厌的副作用,它会使catch(...)吞噬 all 异常.包括AV和SO等您永远都不应捕获的内容.如果这对您很重要,请查看__try/__except和_set_se_translator().

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.

这篇关于MSVC-/EHsc与/EHa(同步与异步异常处理)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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