c – 如何捕获IO异常(确切的IO,而不是std :: exception)

c – 如何捕获IO异常(确切的IO,而不是std :: exception),第1张

概述我尝试了 here的示例程序(使用mingw-w64).该计划崩溃了.所以我编辑了它: #include <iostream> // std::cerr#include <fstream> // std::ifstreamint main(){ std::ifstream file; file.exceptions(std::ifstream::failb 我尝试了 here的示例程序(使用mingw-w64).该计划崩溃了.所以我编辑了它:
#include <iostream>     // std::cerr#include <fstream>      // std::ifstreamint 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

总结

以上是内存溢出为你收集整理的c – 如何捕获I / O异常(确切的I / O,而不是std :: exception)全部内容,希望文章能够帮你解决c – 如何捕获I / O异常(确切的I / O,而不是std :: exception)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1239826.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存