异常处理开关:try{}catch(...){}
2012-10-19 18:27
183 查看
try{}catch(...){};这条语句在我们平时程序中会经常用到,当异常发生时能自动捕获。而对于程序异常时,我们可以通过参数
来设置要捕获对象。
Arguments
a
The exception-handling model that catches asynchronous (structured) and synchronous (C++) exceptions.
s
The exception-handling model that catches C++ exceptions only and tells the compiler to assume that
extern C functions do throw an exception.
c
If used with s (/EHsc), catches C++ exceptions only and tells the compiler to assume that
extern C functions never throw a C++ exception. /EHca is equivalent to
/EHa.
Remarks
Use /EHs to specify the synchronous exception handling model (C++ exception handling without structured exception handling exceptions). If you use
/EHs, then your catch clause will not catch asynchronous exceptions. Also, all objects in scope when the asynchronous exception is generated will not be destroyed even if the asynchronous exception is
handled. Under /EHs, catch(...) will only catch C++ exceptions. Access violations and
System.Exception exceptions will not be caught.
Use /EHa to specify the asynchronous exception handling model (C++ exception handling with structured exception handling exceptions).
/EHa may result in a less performant image because the compiler will not optimize a
try block as aggressively, even if the compiler does not see a throw.
Use /EHa if you want to catch an exception raised with something other than a
throw. The following sample will generate an exception:
该开关位置:
VS2010中如下图
/EH{s|a}[c][-]
来设置要捕获对象。
Arguments
a
The exception-handling model that catches asynchronous (structured) and synchronous (C++) exceptions.
s
The exception-handling model that catches C++ exceptions only and tells the compiler to assume that
extern C functions do throw an exception.
c
If used with s (/EHsc), catches C++ exceptions only and tells the compiler to assume that
extern C functions never throw a C++ exception. /EHca is equivalent to
/EHa.
Remarks
Use /EHs to specify the synchronous exception handling model (C++ exception handling without structured exception handling exceptions). If you use
/EHs, then your catch clause will not catch asynchronous exceptions. Also, all objects in scope when the asynchronous exception is generated will not be destroyed even if the asynchronous exception is
handled. Under /EHs, catch(...) will only catch C++ exceptions. Access violations and
System.Exception exceptions will not be caught.
Use /EHa to specify the asynchronous exception handling model (C++ exception handling with structured exception handling exceptions).
/EHa may result in a less performant image because the compiler will not optimize a
try block as aggressively, even if the compiler does not see a throw.
Use /EHa if you want to catch an exception raised with something other than a
throw. The following sample will generate an exception:
// 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). printf("%d", j); } 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; } }
该开关位置:
VS2010中如下图
相关文章推荐
- C++中异常处理的语法 try catch throw
- java异常处理(Try Catch Finally)
- Java异常处理中try,catch,finally的各种组合用法
- 异常概念和处理机制,try-catch-finally,throw和throws,自定义异常
- js中的异常处理try...catch使用介绍
- C++语言处理异常 try catch throw
- 在.NET里面实现异常处理功能讨论(编写Try/Catch)
- [转贴]从零开始学C++之异常(一):C语言错误处理方法、C++异常处理方法(throw, try, catch)简介
- C++处理异常技巧-try,catch,throw,finally
- Java异常处理综合例子(try、catch、finally、throws、throw)
- JAVA语言如何进行异常处理,关键字:throws,throw,try,catch,finally分别代表什么意义?
- Java异常处理之try...catch...语句的使用进阶
- JAVA异常处理机制的个人理解(try catch throw finally)
- 杂谈异常处理try-catch-finally
- C++处理异常 try,catch,throw
- 10 javaScript的异常处理 try{ }catch(theException){ }
- JAVA语言如何进行异常处理,关键字:throws,throw,try,catch,finally分别代表什么意思?
- 黑马程序员------面向对象(No.7)(内部类、异常概述、异常try-catch、异常声明throws、多异常处理)
- C++中的异常捕获与处理:Try、Throw、Catch关键字的理解和使用
- c#描述异常处理语句try、catch、finally执行时的相互关系