Some day before I faced this question in a interview. So just guide me.
How to clean initialized resources if exception thrown from constructor in c++?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The trick is to use RAII(resource acquisition is initialization) to manage resources.
If you have pointer members then use smart pointers instead of raw pointers which will automagically do the cleanup job once a exception is thrown from constructor.
Good Read:
Herb Sutter’s excellent GotW article “Construction Failures”