You cannot actually catch an exception in a constructor. You can handle it, but you have to rethrow it or another exception. The reason is about object integrity and object lifetimes:
If the construction of a
throws, a part of c
has not been initialized and is completely missing - lifetime of a
never starts. a
is not an optional part of C
, otherwise it had to be a pointer or a std::optional
(since C++14 - boost::optional
before that).
So how do you assemble a C
if one of its vital parts cannot be constructed? You can't. c
can never start to exist as a complete object, so there is no way you can exit the constructor normally. That's the reason why if construction of a member object fails, construction of the whole object has to fail, i.e. has to throw an exception.
If you don't throw an exception in C::C
's catch block, the compiler will do so for you.
C++ Standard, §15.3,15:
The currently handled exception is rethrown if control reaches the end of a handler of the function-try-block of a constructor or destructor.
For a broader treatment on that topic, see http://www.gotw.ca/gotw/066.htm