【发布时间】:2017-03-26 11:32:46
【问题描述】:
我正在为使用 qtestlib、C++(clang LLVM 8.0 版)读取文件编写单元测试。我有以下代码用于逐行读取文件。
std::ifstream infile;
try {
infile.open(path.c_str());
std::ios_base::iostate exceptionMask = infile.exceptions() | std::ios::failbit;
infile.exceptions(exceptionMask);
} catch (std::ios_base::failure& e) {
// print the exception
qDebug() << "Exception caught: " << QString::fromStdString(e.what());
}
try {
std::string line;
while (std::getline(infile, line)) {
// print the line
qDebug() << QString::fromStdString(line);
}
} catch (std::ios_base::failure& e) {
qDebug() << "Exception caught: " << QString::fromStdString(e.what());
}
问题:
上面的代码读取文件中的所有行并打印出来。但是在打印最后一行之后,它会抛出异常并打印以下内容,
捕获的异常:“basic_ios::clear”
我关注了很多线程,但找不到解决方案。为什么会出现此错误?
【问题讨论】:
标签: c++ qt exception-handling