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

Throwing an exception out of a destructor is dangerous. If another exception is already propagating the application will terminate. #include <iostream> class Bad { public: // Added the noexcept(false) so the code keeps its original meaning. // Post C++11 destructors are by default `noexcept(true)` and // this will (by default) call terminate if an exception … Read more

RAII and smart pointers in C++

A simple (and perhaps overused) example of RAII is a File class. Without RAII, the code might look something like this: File file(“/path/to/file”); // Do stuff with file file.close(); In other words, we must make sure that we close the file once we’ve finished with it. This has two drawbacks – firstly, wherever we use … Read more