This documentation is archived and is not being maintained.

Exception Specifications

Exception specifications are used to provide summary information about what exceptions can be thrown out of a function. For example:

void MyFunction(int i) throw(...);

The following table summarizes Visual C++'s implementation of exception specifications:

Exception specification Meaning
throw() The function does not throw an exception.
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 equivalent to throw(...).

If exception handling is used in an application, there must be one or more functions that handle thrown exceptions. Any functions called between the one that throws an exception and the one that handles the exception must be capable of throwing the exception.

The throw behavior of a function depends on the following factors:

  • Whether you are compiling the function under C or C++.
  • Which /EH compiler option you use.
  • Whether you explicitly specify the exception specification.

Explicit exception specifications are not allowed on C functions.

The following table summarizes the throw behavior of a function:

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(...)

Example

// exception_specification.cpp
// compile with: /EHsc
#include <stdio.h>
void handler()
{
   printf("in handler\n");
}

void f1(void) throw(int) 
{
   printf("About to throw 1\n");
   if (1)
      throw 1;
}

void f5(void) throw()
{
   try {
      f1();
   } catch(...) {
      handler();
   }
}

//
// invalid, doesn't handle the int exception thrown from f1()
// void f3(void) throw()   
// {
//   f1();
// }
//

void __declspec(nothrow) f2(void)
{
   try {
      f1();
   } catch(int) {
      handler();
   }
}

// only valid if compiled without /EHc 
// /EHc means assume extern "C" functions don't throw exceptions
extern "C" void f4(void);
void f4(void)
{
   f1();
}

int main()
{
   f2();
   try {
      f4();
   } catch(...) {
      printf("Caught exception from f4\n");
   }
   f5();
}

Output

About to throw 1
in handler
About to throw 1
Caught exception from f4
About to throw 1
in handler

See Also

The try, catch, and throw Statements | C++ Exception Handling

Show: