Export (0) Print
Expand All

Function-try Blocks

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

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;
}

Output

In the catch block

Community Additions

ADD
Show:
© 2014 Microsoft