Function try blocks are a special form of function bodies, for example:
int f() try {
// function body
}
catch {
// one or more catch-clauses.
}
The primary purpose is for usage in constructors, in order to log exceptions thrown by the constructor of any base class. However, it is allowed to use them in regular functions, too.
There exist some (quite old) questions on this, asking why would we need it for regular functions, e.g. Function try blocks, but not in constructors. However, my question is more in slightly in another direction: Can I use it in regular functions as replacement for a regular try-block without concerns? Let's say, just for aesthetical reasons?
I develop a C-Interface for a C++-Library and need to encapsulate each interface-function with a try-block to catch any exceptions. Thus, I would like to avoid an additional curly-bracket-block in each function...
Just one thing, which raised my concerns: In the answer https://stackoverflow.com/a/11535436/6695750, davka cites an article from 2000, claiming that you can't return a value from a catch-block corresponding to a function-try-block. I tested with gcc 5.4.0, there I can return a value from the catch-block without problems. Is this standard, or a non-standard extension of gcc?
is equivalent to
for regular functions.
Only constructor/destructor has special treatment as the catch blocks throw a exception(implicitly or explicitly).
see also the docu here.