我尝试了
here的示例程序(使用mingw-w64).该计划崩溃了.所以我编辑了它:
- #include <iostream> // std::cerr
- #include <fstream> // std::ifstream
- int main()
- {
- std::ifstream file;
- file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
- try {
- file.open("not_existing.txt");
- while (!file.eof())
- file.get();
- file.close();
- }
- catch (std::ifstream::failure e) {
- std::cerr << "Exception opening/reading/closing file\n";
- }
- catch (const std::exception& e) {
- std::cerr << "should not reach this";
- }
- return 0;
- }
现在它运行,但打印不应该达到这个,而我期望它打印异常打开/读取/关闭文件.
为什么我的期望错了?
编辑:
因为这似乎是一个重点,这里是我的编译器的确切版本:mingw-w64版本“x86_64-6.2.0-posix-sjlj-rt_v5-rev1”,即GCC版本6.2
解决方法
这可能是一个MingW错误.我使用MacOS Clang 802.0.42获得了预期的结果.预期的产出是:
Exception opening/reading/closing file
这可能是一个已知的回归:https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66145