I am trying to use streams (specifically cin) to throw exceptions when eof() or fail() are true, but it does not work!! What could I be doing wrong? Does it have to do with can being "tied" to cout??
Thanks in advance,
Juan
1 2 3 4 5 6 7 8 9 10 11 12
cin.exceptions( ios_base::eofbit | ios_base::failbit );
try {
cout << "Enter integer: " << endl;
int val;
cin >> val; // entering a non integer (e.g. ko) should throw exception
}
catch(ios_base::failure& err) {
cout << "error" << '\n';
}
Why would it throw an exception there? You signaled 'eof' and 'fail' to throw an exception. Entering a non-integer shouldn't signal either 'eof' or 'fail'.