You can do this by setting an exception mask before calling open()
std::ifstream inStream;
inStream.exceptions(std::ifstream::failbit);
try {
inStream.open(file_name);
}
catch (const std::exception& e) {
std::ostringstream msg;
msg << "Opening file '" << file_name
<< "' failed, it either doesn't exist or is not accessible.";
throw std::runtime_error(msg.str());
}
By default, none of the thread failure conditions throw exceptions.
source
share