I want to handle exception in a default-value constructed class member. Something like this:
int func()
{
throw 9.0;
return 9;
}
struct T
{
T() try {
}
catch(...)
{
cout << __func__ << endl;
}
int a{func()};
} ;
Is it possible? And what does the latest ISO C++ standard says about it?
EDIT: I actually verified myself it works on most compilers but is it well-defined and should I normally use this construct?
Is it possible?
Yes, that exception would be handled by the function-level handler just like any other thrown by a member's initialiser. Note that, in the function-try-block of a constructor, the exception is rethrown after handling it. This is what you want if the initialisation of a sub-object failed, since the complete object is not valid.
And what does the latest ISO C++ standard says about it?
I don't have C++14 yet, but C++11 said:
15/4: An exception thrown during the execution of the compound-statement or, for constructors and destructors, during the initialization or destruction, respectively, of the class’s subobjects, transfers control to a handler in a function-try-block in the same way as an exception thrown during the execution of a try-block transfers control to other handlers.
and 15.3/15 specifies that it's rethrown in this case.
should I normally use this construct?
Probably not - there's little you can do to handle the failure of a sub-object's initialisation, so usually no point in catching and rethrowing the exception. You might want to report the error as the exception passes through.