I know if an exception is thrown in constructor, destructor will not be called(simple class, no inheritance). So if an exception is thrown in constructor and there is a chance s
Avoid the need to allocate memory on the heap (via new
and new[]
) by using standard library containers. If this is not possible, always use a smart pointer, like std::unique_ptr<>
to manage memory allocated on the heap. Then you will never need to write code for deleting the memory and it will be automatically cleaned up even in case an exception is thrown in your constructor (actually a constructor is often a likely place for an exception, but a destructor should really not throw).