At the core of the fight against exceptions, virtually everything needs to be cleared by destructors. For example, if you are a “new” object, you get a “raw” pointer; if an exception is thrown somewhere, you must make sure that this raw pointer deletes d correctly, but make sure that you do not delete the raw pointer that has not been initialized.
On the other hand, if you store this pointer in std :: unique_ptr, you do not need to do anything; when unique_ptr is destroyed, the object is destroyed, and the destruction of the object occurs automatically: when unique_ptr goes out of scope, the compiler causes a cleanup that is completely invisible (therefore no longer cluttering the code with tons of cleanup calls) and automatically (no more, "when it accepts that a rare path that no one has checked, he forgets to clear ").
The same can be applied to each resource; there is “auto-science” for COM objects (for example, in DirectX, for example), most frameworks should provide you with a “scoped lock” object for wrapping mutexes (therefore, it locks the mutex when creating the object and unlocks it when it is destroyed), and you You can write tiny wrappers to work with various Windows descriptors.
Basically, if you put all your cleanup in destructors, you never have to "try ... catch ... retrieve" just for cleaning. And destructors of "large" objects are often very simple, since almost all "contained" objects are automatically cleared by their destructors.
source share