-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathexception_handling.cpp
More file actions
executable file
·262 lines (223 loc) · 6.64 KB
/
Copy pathexception_handling.cpp
File metadata and controls
executable file
·262 lines (223 loc) · 6.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Demos that mirror docs/exception_handling.md.
// Each demo throws inside its own try/catch so main() stays clean.
#include <bitset>
#include <cmath>
#include <exception>
#include <iostream>
#include <new>
#include <stdexcept>
#include <string>
#include <typeinfo>
#include <vector>
// --- basic try / catch / catch(...) ----------------------------------------
void basicTryCatch() {
try {
throw std::runtime_error("something went wrong");
} catch (const std::runtime_error& e) {
std::cout << "caught runtime_error: " << e.what() << '\n';
} catch (...) {
std::cout << "caught unknown exception\n";
}
}
// --- std::bad_alloc --------------------------------------------------------
void badAllocDemo() {
try {
// Ask for an absurd amount until allocation fails.
while (true) {
new int[100000000ul];
}
} catch (const std::bad_alloc& e) {
std::cout << "bad_alloc: " << e.what() << '\n';
}
}
// --- nothrow new -----------------------------------------------------------
void nothrowDemo() {
// Non-throwing new returns nullptr on failure instead of throwing.
int* p = new (std::nothrow) int[100000000ul];
if (p == nullptr) {
std::cout << "nothrow new returned nullptr\n";
} else {
std::cout << "nothrow new succeeded\n";
delete[] p;
}
}
// --- std::bad_cast ---------------------------------------------------------
struct A { virtual ~A() {} };
struct B { virtual ~B() {} };
void badCastDemo() {
B b;
try {
A& a = dynamic_cast<A&>(b);
(void)a;
} catch (const std::bad_cast& e) {
std::cout << "bad_cast: " << e.what() << '\n';
}
}
// --- std::bad_typeid -------------------------------------------------------
struct Poly { virtual void f() {} };
void badTypeidDemo() {
Poly* p = nullptr;
try {
std::cout << typeid(*p).name() << '\n';
} catch (const std::bad_typeid& e) {
std::cout << "bad_typeid: " << e.what() << '\n';
}
}
// --- std::logic_error ------------------------------------------------------
void logicErrorDemo() {
try {
int amount = 10, available = 9;
if (amount > available)
throw std::logic_error("amount exceeds available");
} catch (std::exception& e) {
std::cout << "logic_error: " << e.what()
<< " (type: " << typeid(e).name() << ")\n";
}
}
// --- std::domain_error -----------------------------------------------------
void domainErrorDemo() {
try {
// acos is only defined on [-1, 1]; 2.0 is outside the domain.
const double x = std::acos(2.0);
std::cout << x << '\n';
} catch (const std::domain_error& e) {
std::cout << "domain_error: " << e.what() << '\n';
}
}
// --- std::invalid_argument -------------------------------------------------
void invalidArgumentDemo() {
try {
// 'X' is not a valid binary digit.
std::bitset<32> bits(std::string("0101001X01010110000"));
} catch (const std::exception& e) {
std::cout << "invalid_argument: " << e.what()
<< " (type: " << typeid(e).name() << ")\n";
}
}
// --- std::length_error -----------------------------------------------------
void lengthErrorDemo() {
try {
std::vector<int> v;
v.resize(v.max_size() + 1);
} catch (const std::length_error& e) {
std::cout << "length_error: " << e.what() << '\n';
}
}
// --- std::out_of_range -----------------------------------------------------
void outOfRangeDemo() {
std::vector<int> v(10);
try {
v.at(20) = 100;
} catch (const std::out_of_range& e) {
std::cout << "out_of_range: " << e.what() << '\n';
}
}
// --- std::overflow_error ---------------------------------------------------
void overflowErrorDemo() {
try {
std::bitset<100> bits;
bits[99] = 1;
bits[0] = 1;
// Result does not fit in unsigned long.
unsigned long n = bits.to_ulong();
(void)n;
} catch (const std::exception& e) {
std::cout << "overflow_error: " << e.what()
<< " (type: " << typeid(e).name() << ")\n";
}
}
// --- std::range_error ------------------------------------------------------
void rangeErrorDemo() {
try {
throw std::range_error("The range is in error!");
} catch (const std::range_error& e) {
std::cout << "range_error: " << e.what() << '\n';
}
}
// --- catch(...) ------------------------------------------------------------
void catchAllDemo() {
try {
throw 42; // not derived from std::exception
} catch (const std::exception& e) {
std::cout << "std::exception: " << e.what() << '\n';
} catch (...) {
std::cout << "catch(...) caught a non-std exception\n";
}
}
// --- user-defined exception ------------------------------------------------
struct CustomException : public std::exception {
const char* what() const noexcept override {
return "CustomException happened";
}
};
void customExceptionDemo() {
try {
throw CustomException();
} catch (const std::exception& e) {
std::cout << "custom: " << e.what() << '\n';
}
}
// --- noexcept and stack unwinding ------------------------------------------
struct Traced {
std::string name;
Traced(std::string n) : name(std::move(n)) {
std::cout << " ctor " << name << '\n';
}
~Traced() noexcept {
std::cout << " dtor " << name << " (unwinding)\n";
}
};
void mayThrow() {
Traced a("a");
Traced b("b");
throw std::runtime_error("boom");
}
void stackUnwindingDemo() {
try {
mayThrow();
} catch (const std::exception& e) {
std::cout << "caught after unwinding: " << e.what() << '\n';
}
}
// --- noexcept function -----------------------------------------------------
void neverThrows() noexcept {
std::cout << "neverThrows() ran\n";
}
void noexceptDemo() {
neverThrows();
}
// --- main ------------------------------------------------------------------
int main() {
std::cout << "== basic try/catch ==\n";
basicTryCatch();
std::cout << "\n== bad_alloc ==\n";
badAllocDemo();
std::cout << "\n== nothrow new ==\n";
nothrowDemo();
std::cout << "\n== bad_cast ==\n";
badCastDemo();
std::cout << "\n== bad_typeid ==\n";
badTypeidDemo();
std::cout << "\n== logic_error ==\n";
logicErrorDemo();
std::cout << "\n== domain_error ==\n";
domainErrorDemo();
std::cout << "\n== invalid_argument ==\n";
invalidArgumentDemo();
std::cout << "\n== length_error ==\n";
lengthErrorDemo();
std::cout << "\n== out_of_range ==\n";
outOfRangeDemo();
std::cout << "\n== overflow_error ==\n";
overflowErrorDemo();
std::cout << "\n== range_error ==\n";
rangeErrorDemo();
std::cout << "\n== catch(...) ==\n";
catchAllDemo();
std::cout << "\n== custom exception ==\n";
customExceptionDemo();
std::cout << "\n== stack unwinding ==\n";
stackUnwindingDemo();
std::cout << "\n== noexcept ==\n";
noexceptDemo();
}