ein*_*ica 5 c++ error-handling fstream ifstream system-error
我想使用 C++ 标准库工具 ( ) 读取文件std::ifstream- 当然,如果遇到错误,也可以可靠地报告错误。
显然,这并不是一件容易的事!
std::basic_fstream的(其模板std::ifstream是实例化)默认情况下不会抛出异常。basic_ios::exceptions()(这是 的超超类std::ifstream)。14年前,有人问过这个问题:
答案告诉我们:
errno/GetLastError()会为我们提供非零/非成功值。那真是太糟糕了。另一方面,14年过去了。有什么改变吗?也就是说,对于抛出的异常或errno/GetLastError()被设置是否有更好的保证?如果不是,那么在构建中报告错误的“尽力而为”方法是什么std::fstream?
(我很想问“为什么构造函数在失败时不会抛出异常?”,但我们不讨论这个。)
这是我现在能想到的最好的事情——“捂住我的屁股”,以防errno万一没有安排好。最坏的情况是我浪费了一些周期重新投入“不愉快的道路”。
// TODO: Consider checking errno here
std::filesystem::path file_path = whatever();
std::ifstream file(file_path, std::ios::binary | std::ios::ate);
try {
file.exceptions(std::ios::failbit | std::ios::badbit);
} catch (std::ios_base::failure& exception) {
if (errno == 0) {
throw;
}
throw std::system_error{ errno, std::generic_category(),
"opening file " + file_path.native());
}
Run Code Online (Sandbox Code Playgroud)