异常捕获--编译开关EHsc、EHa

在vs2008的编译器中有如下编译选项,设置的地方如下图


官方介绍地址

http://msdn.microsoft.com/en-us/library/1deeycx5(v=vs.80).aspx

参数说明

a

The exception-handling model that catches asynchronous(structured) and synchronous (C++) exceptions.

s

    The exception-handling model that catchesC++ exceptions only and tells the compiler to assume that extern C functions do throw an exception.

c

    If used with s (/EHsc), catches C++ exceptionsonly and tells the compiler to assume that extern C functions never throw a C++ exception. /EHca is equivalent to /EHa.

 

官方说明如下:

Use /EHs to specify the synchronous exceptionhandling model (C++ exception handling without structured exception handlingexceptions). If you use /EHs,then your catch clause will not catch asynchronous exceptions. Also, in VisualC++ 2005, all objects in scope when the asynchronous exception is generatedwill not be destroyed even if the asynchronous exception is handled. Under /EHscatch(...) will only catch C++ exceptions. Accessviolations and System.Exception exceptionswill not be caught.

Use /EHa to specify the asynchronous exceptionhandling model (C++ exception handling with structured exception handlingexceptions). /EHa may result in a less performant imagebecause the compiler will not optimize a catch block as aggressively, even ifthe compiler does not see a throw.

Use /EHa if you want to catch an exceptionraised with something other than a throw. The following sample will generate anexception:

// compiler_options_EHA.cpp
// compile with: /EHa
#include <iostream>
#include <excpt.h>
using namespace std;
 
void fail() {   // generates SE and attempts to catch it using catch(...)
   try {
      int i = 0, j = 1;
      j /= i;   // This will throw a SE (divide by zero).
   }
   catch(...) {   // catch block will only be executed under /EHa
      cout<<"Caught an exception in catch(...)."<<endl;
   }
}
 
int main() {
   __try {
      fail(); 
   }
 
   // __except will only catch an exception here
   __except(EXCEPTION_EXECUTE_HANDLER) {   
   // if the exception was not caught by the catch(...) inside fail()
      cout << "An exception was caught in __except." << endl;
   }
}

The /EHc option requires that /EHs or /EHa is specified. Using /clr (CommonLanguage Runtime Compilation) implies /EHa (/clr /EHa is redundant). The compiler willgenerate an error if/EHs[c] isused after /clr.Optimizations will not affect this behavior. When an exception is caught, thecompiler invokes the class destructor or destructors for the object or objectsthat are in the same scope as the exception. When an exception is not caught,those destructors are not run.

See _set_se_translator forexception handling restrictions under /clr.

The option can be cleared by the symbol -. For example, /EHsc- is interpreted as /EHs /EHc- and is equivalent to /EHs.

See Exception Handling: Default Synchronous Exception Model for more information.

To set this compiler option in the Visual Studio developmentenvironment

1.      Open the project's Property Pages dialog box. For details, see How to: OpenProject Property Pages.

2.      Click the C/C++ folder.

3.      Click the Code Generation property page.

4.      Modify the Enable C++ Exceptions property.

Alternately, you can use the following procedure:

To set this compiler option in the Visual Studio developmentenvironment

1.      Click the C/C++ folder.

2.      Click the Code Generation property page.

3.      Set Enable C++ Exceptions to No.

4.      Click the Command Line property page.

5.      Type the compiler option inthe Additional Options box.

To set this compiler option programmatically

·        See ExceptionHandling.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值