`
aigo
  • 浏览: 2568733 次
  • 性别: Icon_minigender_1
  • 来自: 宜昌
社区版块
存档分类
最新评论

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

c++ 
阅读更多

 

官方介绍地址

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.

分享到:
评论

相关推荐

    VC的编译开关

    通过使用 VC 的编译开关 `/EHsc`、`/EHa` 和 `/EHc`,开发者可以根据项目的实际需求选择合适的异常处理模型。这些开关不仅有助于确保程序在面对各种潜在异常时的行为可预测,还能够在一定程度上提高程序的健壮性和可...

    3.用CL.exe编译C程序

    - `/EHsc`:启用异常处理模式,限制为C++标准异常。 - `/Od`:禁用优化,便于调试。 - `/Zi`:生成调试信息,便于使用调试器。 - `/Fe:`:指定输出文件名,例如`/Fe:myprog.exe`。 - `/I`:添加包含目录,例如`/I"C:...

    c++ try catch.doc

    cl /EHsc /EHa source.cpp ``` - 这样做可以告诉编译器不要优化掉`try-catch`块,即使在Release模式下也能确保异常得到妥善处理。 #### 五、`catch(...)`的使用 在C++中,`catch(...)`是一种特殊形式的`catch`...

    CL_exe的全部命令开关\选项说明

    - `/EHsc`:启用C++异常处理模型,限制异常范围。 3. 链接选项: - `/link &lt;options&gt;`:传递给链接器的选项,如指定库、设置入口点等。 - `/SUBSYSTEM:&lt;value&gt;`:指定可执行文件的子系统,如CONSOLE或WINDOWS。 ...

    C++ 命令行开发必知必会

    - `/EHa`: 启用C++和SEH异常处理。 - `/EHc`: 启用C++异常处理,并禁用SEH。 - `/fp:&lt;mode&gt;`: 指定浮点运算模式,其中`mode`可以是`except[-]`、`fast`、`precise`或`strict`。例如,`/fp:fast`选择最快的浮点运算...

    Visual studio 编译器选项

    - `/EHa`:C++异常处理,无C++终止处理,并且与C运行时兼容。 **示例用法**:`cl /EHsc file.cpp` --- ##### /EP(不使用#line指令预处理到stdout) **功能描述**:类似于`/E`,但不会包含`#line`指令的输出。这...

    VC编译器选项

    **2.10 /EHa:启用异步C++异常处理** - **描述**:启用异步的C++异常处理机制。 - **用途**:处理非同步发生的异常。 **2.11 /Gd:__cdecl调用约定** - **描述**:指定函数的调用约定为__cdecl。 - **用途**:控制...

    如何从命令行编译本机 C++ 程序

    `cl /EHsc simple.cpp`这行命令告诉编译器启用C++异常处理,并编译`simple.cpp`。`/EHsc`选项是关于异常处理的设置,它确保编译器支持标准C++异常。 编译成功后,会生成一个可执行文件`simple.exe`。通过`dir ...

    一个VC++用来异常处理的演示代码

    在C++编程中,异常处理是一项重要的错误管理机制,它允许程序员在程序执行过程中捕获并处理意外情况,而不是让程序崩溃。这个“VC++用来异常处理的演示代码”可能是为了展示如何在Microsoft Visual C++环境下有效地...

    VC++编译选项详解

    `/EHsc` 控制异常处理模式,`sc` 表示标准C++异常处理。`/MD` 和 `/MT` 分别指定链接到多线程运行时库的动态版本和静态版本。 四、警告级别与错误控制 `/W0` 到 `/W4` 控制警告级别,其中 `/W4` 显示所有警告。`/WX...

    C++学习官方文档,方便大家学习

    - **编译**:将源代码转换为机器可理解的语言。 - **链接**:将多个目标文件组合成单一可执行文件。 - **翻译单元**:预处理器处理后的源代码文件。 - **MSVC 工具集**:Microsoft 提供的 C++ 编译器及支持工具...

    浅谈C++ 异常处理的语义和性能

    3. **C++ 加SEH**(/EHa):这种模式同时支持C++异常和Windows的SEH异常,因此可以捕获更多类型的错误,包括系统级别的异常。这会进一步增加代码大小,因为需要处理更多的异常情况,但提高了程序的健壮性。 在讨论...

    cl编译器参数在bat命令行指导.txt

    - `/EHsc`:启用C++异常处理,但在C函数中禁用。 - `/DOMITBAD`:定义预处理器宏`MITBAD`,可以在源代码中使用。 - `/nologo`:禁止显示版权和版本信息。 3. **链接文件**: - `cl /FeCWE23_s03 *.obj /I"..\.....

    cuda在vs2008上的配置,QT在VS2008上的配置,以及两者在vs2008共同编译环境的配置

    在“Command Line”中输入NVCC的编译命令,比如Release模式下的编译命令:“$(CUDA_BIN_PATH)\nvcc.exe” -ccbin "$(VCInstallDir)bin" -c -DWIN32 -D_CONSOLE -D_MBCS -Xcompiler /EHsc,/W3,/nologo, ... ...

    MSVC编译器

    - `/EHsc`:启用C++异常处理模型。 - `/std:c++latest`:指定使用最新的C++标准。 **4. 配置编译环境** 设置环境变量`INCLUDE`、`LIB`和`LIBPATH`来指向头文件和库文件的位置,这对于正确编译和链接依赖库的项目至...

    Qt5.15.12,Window10动态库编译64位

    在本文中,我们将深入探讨如何在Windows 10操作系统上使用Qt 5.15.12版本编译64位动态库。Qt是一个流行的开源跨平台应用程序开发框架,广泛用于创建图形用户界面(GUI)和其他类型的应用程序。Qt 5.15.12是该框架的...

    让vc6支持new 抛出异常

    在项目的C/C++属性页中,检查“C++” -&gt; “代码生成” -&gt; “运行时库”是否选择“多线程异常处理 (/EHsc)”。 通过以上步骤,我们就可以在VC6中实现`new`失败时抛出异常的功能,从而增强程序的错误处理能力,使其更...

    opencv在vs项目中配置单

    OpenCV 的核心功能包括图像与视频捕获、图像与视频的分析、特征检测与识别等,广泛应用于机器学习、机器人视觉、医学影像分析等多个领域。 #### 二、配置环境介绍 本文将详细介绍如何在 Visual Studio 2010 (VS2010...

    vc编译参数

    - **/GX[-]**:同/EHsc,用于启用C++异常处理机制。 综上所述,VC编译参数为开发者提供了广泛的选择,可以根据项目需求、目标平台和预期性能,灵活调整代码生成策略。正确理解和合理应用这些参数,对于编写出既高效...

    snmp.rar_Because...

    当项目不启用C++的异常处理机制(即编译时没有开启`/EHsc`等异常支持选项),而JSONCPP库尝试使用异常处理时,就可能会触发编译警告4530。这个警告通常表示在没有异常处理的上下文中使用了异常相关的函数,可能导致...

Global site tag (gtag.js) - Google Analytics