Some pitfalls
Destructors are wonderful. They allow us to automate tasks, they simplify code and they make it safer in general. Still, there are some caveats, some aspects of using destructors that require particular attention.
Destructors should not throw
The title of this section says it quite simply: destructors should not throw. They can throw, but it’s a bad idea to do so.
That might seem surprising at first. After all, constructors can (and do!) throw exceptions. When a constructor throws, it means that the constructor cannot satisfy its postconditions: the object under construction was not constructed (the constructor did not complete!) so that object does not exist. That’s a simple, working model.
If a destructor throws… well, it’s probably the end of your program. Indeed, destructors are implicitly noexcept, which means that throwing from a destructor will call std::terminate() and that will be the end of your program.
Well, you might...