An exception was not found when opening a nonexistent file using C ++

I ran MWE from here: http://www.cplusplus.com/reference/ios/ios/exceptions/ On my machine this is no exception. Here is my code

#include <iostream> #include <fstream> int main() { std::ifstream file; file.exceptions( std::ifstream::failbit | std::ifstream::badbit ); try { file.open("IDoNotExist.txt"); } catch(const std::ifstream::failure& e) { std::cout << "Bad luck!" << std::endl; } } 

Using gcc 6.2.1 on Arch-Linux, I get:

ending a call after calling the instance of 'std :: ios_base :: failure'

what (): basic_ios :: clear

However, the link above mentions that the code should also catch the exception associated with opening the file. Something went wrong?

+5
source share
1 answer

Sounds like a known bug in libstdc ++ .

The problem is that when changing C ++ 11 ABI, many classes were duplicated in libstdc++6.so , one version with the old ABI, the other with the new one.

Exception classes were not duplicated, so this problem did not exist at that time. But then, with some new processing of the language, it was decided that std::ios_base::failure should be deduced from std::system_error instead of std::exception ... but system_error is only a C ++ 11 class, so it should use new ABI or he will complain. Now you have two different classes of std::ios_base::failure and the mess is in your hands!

A simple solution is to compile your program with -D_GLIBCXX_USE_CXX11_ABI=0 and resign to the old ABI until the error is resolved. Or alternatively write catch (std::exception &e) .

+1
source

Source: https://habr.com/ru/post/1258765/


All Articles