In a typical RAII example for input / output of files on Wikipedia, any errors that occur when closing a file are swallowed:
#include <iostream>
#include <string>
#include <fstream>
#include <stdexcept>
void write_to_file (const std::string & message) {
std::ofstream file("example.txt");
if (!file.is_open())
throw std::runtime_error("unable to open file");
file << message << std::endl;
}
There seems to be no way to determine if an error occurred while closing file; obviously, it is possible to call only file.rdstate(), but file- in the scope.
I could call file.close()manually and then check for an error, but I would have to do it in every place that I return from the scope, which defeats the RAII target.
, , , , , AFAIK , .
, RAII , ? , , .
, , - , , . , , ios_base::register_callback. , .
, , ?
, , , , try/catch .