zoukankan      html  css  js  c++  java
  • C++ Exception Handling

    Whole topic can be found: C++ Exception Handling

    1. C++ Exception Specification(http://msdn.microsoft.com/en-us/library/wfa0edys%28VS.80%29.aspx)

    Visual C++ departs from the ANSI Standard in its implementation of exception specifications. The following table summarizes the Visual C++ implementation of exception specifications:

    Exception specification Meaning

    throw()

    The function does not throw an exception. However, if an exception is thrown out of a function marked throw(), the Visual C++ compiler will not call unexpected (see unexpected (CRT) and unexpected (<exception>) for more information). If a function is marked with throw(), the Visual C++ compiler will assume that the function does not throw C++ exceptions and generated code accordingly. Due to code optimizations that maybe performed by the C++ compiler (based on the assumption that the function does not throw any C++ exceptions) if function does throw an exception, the program may not execute correctly.

    throw(...)

    The function can throw an exception.

    throw(type)

    The function can throw an exception of type type. However, in Visual C++ .NET, this is interpreted as throw(...). See Function Exception Specifiers.


    Function /EHsc /EHs /EHa /EHac

    C function

    throw()

    throw(...)

    throw(...)

    throw(...)

    C++ function with no exception specification

    throw(...)

    throw(...)

    throw(...)

    throw(...)

    C++ function with throw() exception specification

    throw()

    throw()

    throw(...)

    throw(...)

    C++ function with throw(...) exception specification

    throw(...)

    throw(...)

    throw(...)

    throw(...)

    C++ function with throw(type) exception specification

    throw(...)

    throw(...)

    throw(...)

    throw(...)

    2. C++ Exception Handling

    Important Note: The Win32 structured exception-handling mechanism works with both C and C++ source files. However, it is not specifically designed for C++. You can ensure that your code is more portable by using C++ exception handling. Also, C++ exception handling is more flexible, in that it can handle exceptions of any type. For C++ programs, it is recommended that you use the C++ exception-handling mechanism (try, catch, throw)

    The real power of C++ exception handling lies not only in its ability to deal with exceptions of varying types (you can throw any type info and catch any type correspondingly), but also in its ability to automatically call destructor functions during stack unwinding, for all local objects constructed before the exception was thrown. 

    C++ exception handling is thread safe. Throwing on one thread (including rethrow) has no impact on any throw/catch in progress on another thread.

    Catchable Types

    Because C++ enables you to throw exceptions of any type, you need to determine which catch handlers can catch an exception of a specific class type. A C++ exception can be caught by a catch handler that specifies the same type as the thrown exception, or by a handler that can catch any type of exception. An exception can also be caught by a catch handler that uses a reference to the same type as the thrown exception.

    If the type of thrown exception is a class, which also has a base class (or classes), it can be caught by handlers that accept base classes of the exception's type, as well as references to bases of the exception's type. Note that when an exception is caught by a reference, it is bound to the actual thrown exception object; otherwise, it is a copy (much the same as an argument to a function).

    Visual C++ Compiler Options
    /EH (Exception Handling Model)

    Key note:

    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.

    • 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 catch 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.

    Default Synchronous Exception Model

    In previous versions of Visual C++, the C++ exception handling mechanism supported asynchronous (hardware) exceptions by default. Under the asynchronous model, the compiler assumes any instruction may generate an exception.

    With the new synchronous exception model, now the default, exceptions can be thrown only with a throw statement. Therefore, the compiler can assume that exceptions happen only at a throw statement or at a function call. This model allows the compiler to eliminate the mechanics of tracking the lifetime of certain unwindable objects, and to significantly reduce the code size, if the objects' lifetimes do not overlap a function call or a throw statement. The two exception handling models, synchronous and asynchronous, are fully compatible and can be mixed in the same application.

    See /EH for information on enabling exception handling.

    Exception Handling Differences

    The major difference between structured exception handling and C++ exception handling is that the C++ exception handling model deals in types, while the C structured exception handling model deals with exceptions of one type — specifically, unsigned int. That is, C exceptions are identified by an unsigned integer value, whereas C++ exceptions are identified by data type. When an exception is raised in C, each possible handler executes a filter that examines the C exception context and determines whether to accept the exception, pass it to some other handler, or ignore it. When an exception is thrown in C++, it may be of any type.

    A second difference is that the C structured exception handling model is referred to as "asynchronous" in that exceptions occur secondary to the normal flow of control. The C++ exception handling mechanism is fully "synchronous," which means that exceptions occur only when they are thrown.

    If a C exception is raised in a C++ program, it can be handled by a structured exception handler with its associated filter or by a C++ catch handler, whichever is dynamically nearer to the exception context. For example, the following C++ program raises a C exception inside a C++ try context:

     Example
    // exceptions_Exception_Handling_Differences.cpp
    // compile with: /EHa
    #include <iostream>

    using namespace std;
    void SEHFunc( void );

    int main() {
    try {
    SEHFunc();
    }
    catch( ... ) {
    cout << "Caught a C exception."<< endl;
    }
    }

    void SEHFunc() {
    __try {
    int x, y = 0;
    x = 5 / y;
    }
    __finally {
    cout << "In finally." << endl;
    }
    }

    Output

    In finally.
    Caught a C exception. (Catch(...) should not be executed when /EHs)
    And more:
    Structured Exception Translator
    Win32 reports serious programming errors such as attempts to access protected or non-existent memory locations or attempts to divide by integer zero
    by throwing so-called
    structured exceptions.
    In C++, more benign error conditions that occur at runtime result in C++ exceptions.
    In general, it is a mistake to conflate the two concepts. Their only connection lies in the fact that C++ exceptions are implemented by means of structured
    exceptions.
    Nevertheless, during the debugging phase of a project it is often expedient to have structured exceptions generate C++ exceptions.
    The sample below demonstrates the translation of structured exceptions to C++ exceptions.
    Get it here:
    http://www.ivrforbeginners.com/downloads/downloads.htm

    More From MSDN about SEH
    Structured Exception Handling

    An exception is an event that occurs during
    the execution of a program, and requires the execution of code outside
    the normal flow of control. There are two kinds of exceptions: hardware
    exceptions and software exceptions. Hardware exceptions are
    initiated by the CPU. They can result from the execution of certain
    instruction sequences, such as division by zero or an attempt to access
    an invalid memory address. Software exceptions are initiated
    explicitly by applications or the operating system. For example, the
    system can detect when an invalid parameter value is specified.


    Structured exception handling is a mechanism for handling
    both hardware and software exceptions. Therefore, your code will handle
    hardware and software exceptions identically. Structured exception
    handling enables you to have complete control over the handling of
    exceptions, provides support for debuggers, and is usable across all
    programming languages and machines. Vectored exception handling is an extension to structured exception handling.

  • 相关阅读:
    彭明辉教授-《研究生完全求生手册》
    使用npm创建一个命令行工具
    #!/usr/bin/python与#!/usr/bin/env python的区别
    使用node+express搭建第一个node后端项目
    使用npm创建一个程序库包
    多项式多点求值
    2. 两数相加
    CSS实现子元素自动充满父元素的剩余空间
    侧边导航栏滚动条---CSS overflow实现
    Asp.NET Core简介
  • 原文地址:https://www.cnblogs.com/taoxu0903/p/1641629.html
Copyright © 2011-2022 走看看