【发布时间】:2017-12-24 19:14:03
【问题描述】:
我的代码在到达 eof 并抛出异常时设置了 std::failbit 如何跳过 eof 异常
在 catch 块中,我检查并跳过异常是否是因为 eof 但它不好。
请建议我如何在下面的代码中跳过 eof 异常
std::ifstream in
std::string strRead;
in.exceptions ( std::ifstream::failbit | std::ifstream::badbit );
try
{
while (getline( in,strRead))
{
//reading the file
}
catch(std::ifstream::failure & exce)
{
if(! in.eof()) // skip if exception because of eof but its not working?
{
cout<<exce.what()<<endl;
return false;
}
}
catch(...)
{
cout("Unknow exception ");
return false;
}
【问题讨论】:
-
@CristianIonescu 不,请阅读我的直到结束的问题,我担心我是否可以在 catch 块中处理?
-
@CristianIonescu 没有与我的问题相关的地方
-
请提供MVCE。
-
@CristianIonescu 你没有分享的链接在哪里谈到在我的代码执行的 catch 块中处理 eof 异常,这就是我所有的问题。我请求你阅读我所有的问题并比较你分享的那个
标签: c++ file exception-handling