...
In this noncompliant code example, a std::fstream
object file
is constructed. The constructor for std::fstream
calls std::basic_filebuf<T>::open()
, and the default std::terminate_handler
called by std::terminate()
is std::abort()
, which does not call destructors. Consequently, the underlying std::basic_filebuf<T>
object maintained by the object is not properly closed, and the program has no way of determining whether an error occurs while flushing or closing the file.
Code Block | ||||
---|---|---|---|---|
| ||||
#include <exception> #include <fstream> #include <string> void f(const std::string &fileName) { std::fstream file(fileName); if (!file.is_open()) { // Handle error return; } // ... std::terminate(); } |
...