If you shouldn’t throw exceptions in a destructor, how do you handle errors in it?

Most people say never throw an exception out of a destructor – doing so results in undefined behavior. Stroustrup makes the point that “the vector destructor explicitly invokes the destructor for every element. This implies that if an element destructor throws, the vector destruction fails… There is really no good way to protect against exceptions … Read more

Do I need to explicitly call the base virtual destructor?

When overriding a class in C++ (with a virtual destructor) I am implementing the destructor again as virtual on the inheriting class, but do I need to call the base destructor? If so I imagine it’s something like this… MyChildClass::~MyChildClass() // virtual in header { // Call to base destructor… this->MyBaseClass::~MyBaseClass(); // Some destructing specific … Read more

How do I correctly clean up a Python object?

class Package: def __init__(self): self.files = [] # … def __del__(self): for file in self.files: os.unlink(file) __del__(self) above fails with an AttributeError exception. I understand Python doesn’t guarantee the existence of “global variables” (member data in this context?) when __del__() is invoked. If that is the case and this is the reason for the exception, … Read more