Just handle the exceptions inside a catch all block and rethrow the exception:
MyClass::MyClass() :
member1(NULL)
,member2(NULL)
{
try
{
member1 = new Member1Type;
member2 = new Member2Type;
}
catch (...)
{
delete member2;
delete member1;
throw();
}
}
Because freeing the memory is usually also done in the destructor, it is common to write a cleanup function that can be called from the destructor and within the above
catch
block.