Context
I have this simple code:
#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; }
Just to complete, this is a compilation command:
g++ -std=c++11 -g // ...
G ++ Compiler Version (GCC) 6.1.1.
Platform: arch-linux 4.7.2-1.
Problem
As you can imagine, the file does not exist, therefore the file.open(...) method file.open(...) an exception. The problem is that when I run the code, the exception is not handled and std::terminate called.
The strange thing is the result:
terminate called after throwing an instance of 'std::ios_base::failure' what(): basic_ios::clear Annullato (core dump creato)
As you can read, the throwing class is std::ios_base::failure , but my catch is right in this class.
My question is: what am I missing?
c ++ exception-handling c ++ 11
Biagio festa
source share