在当今的 C++ 中,构造 fstream 时我能可靠地得到错误吗?

ein*_*ica 5 c++ error-handling fstream ifstream system-error

我想使用 C++ 标准库工具 ( ) 读取文件std::ifstream- 当然,如果遇到错误,也可以可靠地报告错误。

显然,这并不是一件容易的事!

  • std::basic_fstream的(其模板std::ifstream是实例化)默认情况下不会抛出异常。
  • 您可以使基本的 fstream 抛出异常 - 但只能在构造之后,因此构造不会失败。请参阅basic_ios::exceptions()(这是 的超超类std::ifstream)。

14年前,有人问过这个问题:

获取 std::fstream 失败错误消息和/或异常

答案告诉我们:

  1. 不能保证抛出的异常会告诉我们错误的原因是什么(只是发生了一些错误)
  2. 我们不能保证当在 fstream 上设置失败位或错误位时,errno/GetLastError()会为我们提供非零/非成功值。

那真是太糟糕了。另一方面,14年过去了。有什么改变吗?也就是说,对于抛出的异常或errno/GetLastError()被设置是否有更好的保证?如果不是,那么在构建中报告错误的“尽力而为”方法是什么std::fstream

(我很想问“为什么构造函数在失败时不会抛出异常?”,但我们不讨论这个。)

ein*_*ica 1

这是我现在能想到的最好的事情——“捂住我的屁股”,以防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)