A function-try-block is equivalent to a try block inside the function spanning the whole function unless you have a constructor or destructor.
On a constructor, the function-try-block also catches exceptions thrown by constructors of your base classes and non-static data members. Catching those is not possible with a regular try block spanning the statement-block of the constructor. This is the primary use-case for function-try-block.
On a destructor, the function-try-block also catches exceptions thrown by destructors of base classes and your non-static data members. This cannot be achieved using a try block inside the destructor. Note that destructors that throw are bad design, nonetheless they are legal in C++ and this is the way to deal with them.
In both these cases, additional rules apply: You cannot use return
in the function-try-block’s catch
clauses of a constructor because a data member may not be properly constructed; however, you may use throw
and at the end of every catch
, there is an implicit throw;
statement. In a destructor’s function-try-block, at the end of every catch
, there is also an implicit throw;
, but an explicit return;
is allowed.