Export (0) Print
Expand All
Expand Minimize

/EH (Exception Handling Model)

Specifies the kind of exception handling to be used by the compiler, and destroys C++ objects that go out of scope as a result of an exception. If /EH is not specified, the compiler catches structured exceptions and C++ exceptions, but does not destroy C++ objects that go out of scope as a result of an exception.

/EH{s|a}[c][-]

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.

If you use /EHs, then your catch clause does not catch asynchronous exceptions, and objects in scope when the asynchronous exception is generated are not destroyed even if the asynchronous exception is handled. Access violations and System.Exception exceptions are not caught.

If you use /EHa, the image might not perform as well because the compiler does 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 that's raised by something other than a throw. The following example generates 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;
   }
}

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

See _set_se_translator for exception handling restrictions under /clr.

The option can be cleared by use of 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 development environment

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

  2. Select the C/C++ folder.

  3. Select the Code Generation property page.

  4. Modify the Enable C++ Exceptions property.

    Or, set Enable C++ Exceptions to No, and then on the Command Line property page, in the Additional Options box, add the compiler option.

To set this compiler option programmatically

Show:
© 2014 Microsoft