-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththrow_unexpected.cpp
More file actions
64 lines (49 loc) · 1.28 KB
/
throw_unexpected.cpp
File metadata and controls
64 lines (49 loc) · 1.28 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
// Test throwing an exception that is unexpected
//
// Summary:
// 1. Special function "unexpected()" would be called if an exception thrown is not in the specialization list
// 2. Use the special function "set_unexpected()" to customize the "unexpected()" function
//
//
// Test Output:
// up caught
// fit caught
// unexpected exception thrown
#include <iostream>
#include <exception>
#include <cstdlib>
#include <string>
using namespace std;
class up {};
class fit {};
void g();
void f(int i) throw(up, fit) {
switch(i) {
case 1: throw up(); // exit from throw
case 2: throw fit(); // exit from throw
}
g(); // can throw an exception that is not in the specialization list
}
//void g() {} // versio 1, no exception thrown
void g() { // version 2, can throw built-in types
throw 47;
}
void my_unexpected() {
cout << "unexpected exception thrown" << endl;
exit(1);
}
int main() {
set_unexpected(my_unexpected);
for (int i = 1; i <= 3; ++i) {
try {
f(i);
} catch(up) {
cout << "up caught" << endl;
} catch(fit) {
cout << "fit caught" << endl;
} catch(...) {
cout << "unknown caughted" << endl;
}
}
return 0;
}