...
A better approach would be to employ RAII. This forces every object to 'clean up after itself' in the face of abnormal behavior, preventing the programmer from having to do so. A judicious autounique_ptr
would free the resource whether an error occurs or not.
Code Block | ||
---|---|---|
| ||
while (moreToDo) { std::autounique_ptr<SomeType> pst = new SomeType(); try { pst->processItem(); } catch (...) { // deal with exception throw; // pst automatically freed } // pst automatically freed } |
...