Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
failure.cpp
Go to the documentation of this file.
1
30#include <iostream>
31#include <stdexcept>
32
33#include <examples.h>
34
35
36#ifndef _RWSTD_NO_EXCEPTIONS
37
38int main ()
39{
40 const std::ios::iostate allbits =
41 std::ios::badbit | std::ios::eofbit | std::ios::failbit;
42
43 // Enable all exceptions in cin.
44 std::cin.exceptions (allbits);
45
46 while (!std::cin.eof ()) {
47
48 // Clear state bits.
49 std::cin.clear ();
50
51 try {
52 short i;
53 char ch;
54
55 // Try to extract a character and an integer.
56 std::cin >> ch >> i;
57
58 std::cout << ch << ' ' << i << '\n';
59 }
60
61#ifndef _RWSTD_NO_EXT_FAILURE
62
63 // Classes badbit_set, eofbit_set, and failbit_set are
64 // derived from failure and are optional extensions of
65 // this implementation.
66
67 catch (const std::ios::badbit_set &e) {
68 std::cout << "Caught std::ios::badbit_set: "
69 << e.what () << '\n';
70 }
71 catch (const std::ios::eofbit_set &e) {
72 std::cout << "Caught std::ios::eofbit_set: "
73 << e.what () << '\n';
74 }
75 catch (const std::ios::failbit_set &e) {
76 std::cout << "Caught std::ios::failbit_set: "
77 << e.what () << '\n';
78 }
79
80#endif // _RWSTD_NO_EXT_FAILURE
81
82 catch (const std::ios::failure &e) {
83 std::cout << "Caught std::ios::failure: "
84 << e.what () << '\n';
85 }
86 catch (const std::exception &e) {
87 std::cout << "Caught std::exception: "
88 << e.what () << '\n';
89
90 return 1; // Indicate error.
91 }
92 catch (...) {
93 std::cout << "Caught an unknown exception\n";
94
95 return 1; // Indicate error.
96 }
97 }
98
99 return 0;
100}
101
102#else // if defined (_RWSTD_NO_EXCEPTIONS)
103
104int main ()
105{
106 std::cout << "Exceptions not supported\n";
107
108 return 0;
109}
110
111
112#endif // _RWSTD_NO_EXCEPTIONS
#define i
Definition 17.names.cpp:126
#define e
Definition 17.names.cpp:118
int main()
Definition failure.cpp:38