forked from maniacs-satm/cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexceptions.cpp
More file actions
259 lines (243 loc) · 4.72 KB
/
exceptions.cpp
File metadata and controls
259 lines (243 loc) · 4.72 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
#include <iostream>
#include <typeinfo>
#include <stdexcept>
#include <memory>
using namespace std;
class D
{
int i;
public:
D() : i(0)
{
if(i == 1)
{
throw std::runtime_error("Exception in ctor");
}
cout << "D::D()" << endl;
}
D(int _i) : i(_i) { cout << "D::D(int)" << endl; }
~D()
{
if(i == 3)
{
// Gets warning int C++98
throw std::runtime_error("Exception in dtor");
}
cout << "D::~D()" << endl;
}
void set(int _i) { i = _i;}
int get() const { return i; }
};
class simpleException
{
public:
simpleException() { cout << "init exception" << endl; }
~simpleException() { cout << "Dtor of an exception" << endl; }
void what() const { cout << "WTF?" << endl; }
};
// Regenerate throw
bool isFixed = false;
void letstry()
{
cout << "Before try" << endl;
try
{
cout << "Inside try: begin" << endl;
if (!isFixed)
{
throw simpleException();
}
else
{
cout << "Fixed!" << endl;
}
cout << "Inside try: end" << endl;
}
catch(const simpleException& e)
{
e.what();
cout << "Generated: " << typeid(e).name() << endl;
if (!isFixed)
{
isFixed = true;
throw;
}
}
}
void challenge1()
{
try
{
letstry();
}
catch (...)
{
if (isFixed)
{
cout << "Everything is okay" << endl;
}
else
{
cout << "Still not fixed! terminate()" << endl;
}
}
}
template<typename T>
void lestMakeLeak2_1(int i)
{
T* p = new T();
if(i == 0)
{
throw std::logic_error("Ruine challenge2");
}
delete p;
}
template<typename T>
void lestMakeLeak2_2(int i)
{
std::auto_ptr<T> p(new T());
if (i == 0)
{
throw std::logic_error("Ruine challenge2");
}
}
// Check if T call dtor
template<typename T>
void challenge2()
{
try {
// Dtor called
lestMakeLeak2_1<T>(10);
// Dtor could not be called => memory leak
lestMakeLeak2_1<T>(0);
} catch (...) {
cout << "Catched!" << endl;
}
// using smart pointer
try {
// Dtor called
lestMakeLeak2_2<T>(10);
// Dtor also called
lestMakeLeak2_2<T>(0);
} catch (...) {
cout << "Catched!" << endl;
}
}
// Test auto_ptr
template<typename T>
void challenge3()
{
auto_ptr<T> p1(new T());
auto_ptr<T> p2 = p1;
// Segmentation Fault
// cout << p1.get()->getIndex() << endl;
// p2 owned new T(), p1 == NULL
cout << p2.get()->getIndex() << endl;
const auto_ptr<T> cp1(new T());
// auto_ptr<T> cp2(cp1); // - compilation error
// cp1.release(); // - compilation error
}
// Exceptions in ctors
template<typename T>
void challenge4()
{
try
{
D obj(1);
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
}
// Exceptions in dtors
template<typename T>
void challenge5()
{
try
{
// std::unexpected()
// B obj(T(1));
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
}
// Exceptions and methods
template<typename T>
void f_throw(int ex_id) throw(std::logic_error, simpleException)
{
T test(1);
if (ex_id == 1)
{
throw std::logic_error("Test std::logic_error");
}
if (ex_id == 2)
{
throw simpleException();
}
// std::unexpected
if(ex_id == 3)
{
throw std::runtime_error("std::unexpected");
}
}
// Exceptions f() throw(...) {}
template<typename T>
void challenge6()
{
try
{
// f_throw(1); // logic_error
// f_throw(2); // simpleException
f_throw<T>(3); // std::unexpected
}
catch(const std::logic_error& le)
{
std::cout << le.what() << std::endl;
}
catch(const simpleException& se)
{
se.what();
}
}
// Exceptions in initializer list
template<typename T>
void challenge7() {
try
{
D obj(3);
}
catch (const std::logic_error& le)
{
std::cout << "Emulation of exception in initializer list failed" << std::endl;
}
}
// yet another bad practice to prevent memory leaks
template<typename T>
void challenge8()
{
T* test;
try
{
test = new T;
throw std::logic_error("Logic error");
}
catch(...)
{
delete test;
}
}
/* -------------------- */
int main() {
// challenge1<D>();
// challenge2<D>();
// challenge3<D>();
// challenge4<D>();
// challenge5<D>();
// challenge6<D>();
// challenge7<D>();
challenge8<D>();
return 0;
}