yo/iostreams/examples/ioexceptions.cc
raw| 1 | #include <iostream> |
| 2 | |
| 3 | using namespace std; |
| 4 | |
| 5 | int main() |
| 6 | { |
| 7 | cout << "Enter a word, not a number: "; |
| 8 | cin.exceptions(ios::failbit); |
| 9 | int |
| 10 | x; |
| 11 | try |
| 12 | { |
| 13 | cin >> x; |
| 14 | } |
| 15 | catch (ios::failure failure) |
| 16 | { |
| 17 | cout << "Here's what: " << failure.what() << '\n'; |
| 18 | } |
| 19 | } |
| 20 | /* |
| 21 | Generated output: |
| 22 | Enter a word, not a number: grandpa |
| 23 | Here's what: basic_ios::clear(iostate) caused exception |
| 24 | */ |