试验I/OI得到一个异常,不应该抛出任何异常:
#include <iostream>
#include <fstream>
int main()
{
std::ifstream f("/tmp");
std::cout << "Exception Flags: " << f.exceptions() << std::endl;
if(f >> std::ws) std::cout << "This will not succeed" << std::endl;
else std::cout << "Ok - it fails" << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但输出是:
Exception Flags: 0
terminate called after throwing an instance of 'std::ios_base::failure'
what(): basic_filebuf::underflow error reading the file
Aborted
Run Code Online (Sandbox Code Playgroud)
g ++(Ubuntu/Linaro 4.7.2-2ubuntu1)4.7.2
编辑
测试应该毫无例外地失败:
从27.5.5.4 basic_ios标志函数
void clear(iostate state = goodbit);
4后置条件:如果rdbuf()!= 0则state == rdstate(); 否则rdstate()==(state | ios_base …