Streams do not throw exceptions by default, but you can tell them to throw exceptions by calling the file.exceptions(~goodbit) function.
Instead, the usual way to detect errors is to simply check the status of the stream:
if (!file) cout << "error!! " << endl ;
The reason for this is that there are many common situations where invalid reading is a secondary issue rather than a major one:
while(std::cin >> input) { std::cout << input << '\n'; }
compared with:
for(;;) { try { std::cin >> input; std::cout << input << '\n'; } catch(...) { break; } }
Watch it live: http://ideone.com/uWgfwj
Mooing duck
source share