我是否应该期望在 CentOS6 和 CentOS7 之间处理 fstream 异常时有不同的行为
Should I expect different behavior with fstream exception handling between CentOS6 and CentOS7?
我正在使用异常处理从带有 fstream 对象的文件中读取数据。由于逻辑的实现方式,代码将在命中文件末尾时抛出 fstream::failure 异常后在 fstream 上执行 tellg()。此执行流程不一定是有意为之,但在 Windows (MSVS 2010) 或 CentOS6 上运行时不会造成任何问题。但是,在 CentOS7 上运行时,我得到了一个核心转储。如果我在 tellg() 之前添加一个调用来清除 fstream,一切都很好。
抛出的错误是:
在抛出"std::ios_base::失败"实例后终止调用 what(): basic_ios::清除
有人可以提供关于这种行为变化是否在意料之中的见解吗?
gcc 和 libstdc++ 的适用版本是:
对于 CentOS6:
- GCC 版本 4.4.7
- 20120313 (Red Hat 4.4.7-11) (GCC) /
- usr/lib64/libstdc++.so.6.0.13
对于 CentOS7:
- GCC 版本 4.8.2
- 20140120 (Red Hat 4.8.2-16) (GCC) /
- usr/lib64/libstdc++.so.6.0.19
下面提供了执行此问题的代码示例:
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
fstream in;
in.exceptions(ifstream::failbit);
cout << "Before open" << endl;
in.open("in.txt", ios::in);
cout << "After open" << endl;
try
{
string s;
while ( 1 )
{
getline(in, s);
cout << s << endl;
}
}
catch(fstream::failure e)
{
cout << "EOF Exception." << endl;
}
catch(...)
{
cout << "Unhandled Exception." << endl;
}
// --- uncomment this to make it work --- in.clear();
in.tellg();
return 0;
}
感谢您的帮助!
如 PR 26211 所示,GCC 4.6.0 实现了 DR 419 的解析,因此新行为是有意为之。
标准中存在缺陷,该缺陷已修复,因此更改了GCC的库以匹配标准中的新规范。
相关文章:
- 处理多个异常集合的C++方法
- 我在c++代码中生成了一个运行时#3异常
- 孤立代码块在结构中引发异常
- C++中的赋值发生,尽管右侧出现异常
- 在fstream期间忽略空行
- 从构造函数抛出异常时如何克服内存泄漏
- 异常属于C++中的线程还是进程
- 当类定义不可见时捕获异常
- 引发异常:读取访问冲突**dynamicArray**为0x1118235.发生
- 为什么异常不退出程序?
- 为什么我应该在异常处理中使用std::cerr而不是std::cout
- 如何修复链表类实现的未处理异常0xDDDDDDDD
- 关于:C++中异常对象的范围:为什么我没有得到副本?
- 我是否应该期望在 CentOS6 和 CentOS7 之间处理 fstream 异常时有不同的行为
- 当"std::fstream"无法打开时会引发异常吗?
- 硬关闭控制台/C++ 后的 fstream 声明异常
- (c++)在title.exe[6284]中出现fstream win32异常
- fstream EOF异常抛出
- fstream打开时未处理异常
- c++ fstream:当到达eof时抛出异常