语境
我有这个简单的代码:
#include <iostream>
#include <fstream>
#include <system_error>
int main(int argc, char *argv[]) {
std::ifstream file;
file.exceptions(std::ios::failbit | std::ios::badbit);
try {
file.open("a_file_does_not_exist.txt", std::ios::in);
file.close();
} catch(const std::ios_base::failure& err) {
std::cerr << err.code() << std::endl;
return -1;
}
return 0;
}
只是为了完成,这是编译命令:
g++ -std=c++11 -g // ...
编译器的版本是g++ (GCC) 6.1.1。
平台:arch-linux 4.7.2-1。
问题
可以想象,该文件不存在,因此该方法file.open(...)
将引发异常。问题是当我运行代码时,未处理异常并被std::terminate
调用。
奇怪的是输出:
terminate called after throwing an instance of 'std::ios_base::failure'
what(): basic_ios::clear
Annullato (core dump creato)
如您所见,投掷类是 a std::ios_base::failure
,但我的发现是正确的。
我的问题是:我错过了什么?