Thursday, November 25, 2010

Function Try-Catch Blocks

Both of the following function declarations compile:

void func1()
{
    try
    { }
    catch (...)
    {}
}

void func2()
try
{ }
catch (...)
{ }

The second one is called a function try/catch block. Here is an example in a constructor:

MyClass::MyClass()
try 
: d_mem1(0), d_mem2 
{ } 
catch (...) 
{ }

References:
Effective STL: 50 Specific Ways to Improve Your Use of the Standard Template Library by Scott Meyers. Addison-Wesley, 2001.
C++ Templates by David Vandevoorde and Nicolai M. Josuttis. Addison-Wesley, 2003
http://www.gotw.ca

No comments:

Post a Comment