I read several articles here and also where it is normal to throw an exception from the constructor. However, I noticed that it does not call the destructor of the base class or its data members if an exception is thrown from the constructor. Consider the following example:
In this case, constructor E throws an exception, but C destructor as a data element or as a base class is not called. Now, if C destructor performs some cleanup operation, for example, closes files / sockets and removes heap allocation, this can cause problems.
So my question is why and when is it normal to throw exceptions from the constructors.
c ++ constructor exception-handling
user236215
source share