Partager via


Function-try Blocks

A function-try block catches exceptions generated from a call made in a constructor's initialization list.

Example

The following sample uses a function-try block:

// exceptions_Function_tryBlocks.cpp
// compile with: /EHsc
#include "stdio.h"

int f(int i) {
   throw "test";
   return 0;
}

class C {
   int i;

public:
   C(int);
};

C::C(int ii) {
   try {   // function-try block
      f(ii) ;
      // body of function goes in try block
   }
   catch (...) {
      // handles exceptions thrown from the constructor-initializer
      // and from the constructor function body
      printf_s("In the catch block\n");
   }
}

int main() {
   C *MyC = new C(0);
   delete MyC;
}

In the catch block

See Also

Concepts

C++ Exception Handling

The try, catch, and throw Statements