-
Notifications
You must be signed in to change notification settings - Fork 0
/
exception.cpp
106 lines (89 loc) · 2.72 KB
/
exception.cpp
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
// exceptions
// - try. catch, throw
// - new, bad_alloc
// - unique_ptr
// - set_unexpected
#include <iostream>
#include <stdexcept>
#include <memory>
#include <new>
#define GB (1024*0x100000L)
using std::cout;
using std::endl;
using std::exception;
using std::bad_alloc;
using std::unique_ptr;
class my_exception : public exception {
public:
my_exception() throw() { cout << "my_exception: constructor\n"; }
virtual ~my_exception() throw() { cout << "my_exception: destructor\n"; }
virtual const char * what() const throw() { return "MYEXCEPTION"; }
};
int func_throw_exception(void)
{
throw exception();
cout << "func_throw_exception: after throw" << endl;
return 0;
}
void my_unexpected_handler(void)
{
cout << "my_unexpected_handler: called, program exitting" << endl;
exit(1);
}
void test_set_unexpected(void) throw()
{
throw my_exception();
}
// ----------------- MAIN -------------------------------------------
int main()
{
// starting
cout << __func__ << ": starting" << endl;
// catch std::exception
try {
func_throw_exception();
cout << "main: after func_throw_exception call, should not be here" << endl;
} catch (exception &e) {
cout << "main: exception caught - " << e.what() << endl;
}
// catch std::bad_alloc
try {
char * p = new char[1000*GB];
cout << "main: after new, should not be here, p=" << p << endl;
} catch (bad_alloc &ba) {
cout << "main: bad_alloc exception caught - " << ba.what() << endl;
}
// catch std::exception, followed by catch of any other exception
try {
char * p = new char[1000*GB];
cout << "main: after new, should not be here, p=" << static_cast<void*>(p) << endl;
} catch (exception &e) {
cout << "main: exception caught - " << e.what() << endl;
} catch (...) {
cout << "main: unknown exception caught, should not be here" << endl;
}
// catch any exception
try {
char * p = new char[1000*GB];
cout << "main: after new, should not be here, p=" << static_cast<void*>(p) << endl;
} catch (...) {
cout << "main: unknown exception caught" << endl;
}
// unique_ptr
try {
unique_ptr<int[]> x (new int[100]);
for (int i = 0; i < 100; i++) {
x[i] = i;
}
throw my_exception();
cout << "main: after throw exception" << endl;
} catch (exception &e) {
cout << "main: exception caught, unique_ptr automatically freed - " << e.what() << endl;
}
// test set_unexpected
std::set_unexpected(my_unexpected_handler);
test_set_unexpected();
// terminating
cout << __func__ << ": terminating" << endl;
return 0;
}