Skip to content

Commit 8c97cc0

Browse files
author
Oguzhan Katli
committed
exception section revisited
1 parent 34bbfdd commit 8c97cc0

File tree

3 files changed

+43
-51
lines changed

3 files changed

+43
-51
lines changed

inc/advanced_cpp_topics.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ CREATE_ELEMENT(TemplateTraits);
5050

5151
// Exceptions
5252
CREATE_ELEMENT(ExceptionUsage);
53+
CREATE_ELEMENT(ExceptionUnwinding);
5354

5455
// Custom Comparators
5556
CREATE_ELEMENT(CustomComparatorNeedExample);

main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ int main(int argc, char *argv[]) {
2929
.Add<StaticUsageExample>()
3030
// Exceptions
3131
.Add<ExceptionUsage>()
32+
.Add<ExceptionUnwinding>()
3233
// CPU Architecture
3334
.Add<CpuCacheExample>()
3435
.Add<CountIfRandom>()

src/6-Exceptions/exception_usage.cpp

Lines changed: 41 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -101,40 +101,27 @@ namespace exceptions {
101101
} // namespace custom
102102

103103
namespace rethrow {
104-
std::exception_ptr eptr; // object to hold exceptions (or nullptr)
105-
void foo()
104+
std::exception_ptr eptr;
105+
void thread_func() try
106106
{
107-
try {
108-
throw new std::runtime_error("asdasd");
109-
}
110-
catch (...) {
111-
eptr = std::current_exception(); // save exception for later processing
112-
}
107+
std::this_thread::sleep_for(std::chrono::seconds(1));
108+
throw std::runtime_error("abort thread");
113109
}
114-
115-
void bar()
116-
{
117-
if (eptr != nullptr) {
118-
std::rethrow_exception(eptr); // process saved exception
119-
}
110+
catch (...) {
111+
eptr = std::current_exception(); // save exception for later processing
120112
}
121113

122-
void usage() {
123-
foo();
124-
try
125-
{
126-
// lately...
127-
bar();
128-
}
129-
catch (const std::exception& ex)
130-
{
114+
void usage()
115+
{
116+
std::thread(thread_func).join();
117+
try {
118+
if (eptr != nullptr) {
119+
// process saved exception
120+
std::rethrow_exception(eptr);
121+
}
122+
} catch (const std::exception& ex) {
131123
std::cout << ex.what() << std::endl;
132-
}
133-
catch (std::exception* ex)
134-
{
135-
std::cout << ex->what() << std::endl;
136-
}
137-
catch (...) {
124+
} catch (...) {
138125
std::cout << "catched unknown exception!" << std::endl;
139126
}
140127
}
@@ -144,45 +131,43 @@ namespace exceptions {
144131
{
145132
struct unwinder
146133
{
147-
virtual int unwind() const = 0;
134+
virtual ~unwinder() = default;
135+
virtual void unwind() const = 0;
148136
};
149137

150138
void will_throw(int should_throw)
151139
{
152-
// x degerinin delete edildigini gostermek icin custom deleter kullaniliyor
153-
std::unique_ptr<int, std::function<void(int*)>> x(new int(7), [&](int *p) {
154-
delete p;
155-
});
156-
struct unwinder_impl : public unwinder
140+
int* val = new int(10);
141+
struct stack_cleaner final
142+
: public unwinder
157143
{
158-
int* val;
159-
unwinder_impl(int* _val) : val(_val) {
160-
++(*val);
161-
printf("%s\n", __FUNCTION__);
162-
}
163-
~unwinder_impl() { printf("%s\n", __FUNCTION__); }
144+
int*& val;
145+
stack_cleaner(int*& _val)
146+
: val(_val) { printf("%s\n", __FUNCTION__); }
147+
~stack_cleaner() { printf("%s\n", __FUNCTION__); }
164148

165-
int unwind() const override
166-
{
167-
// val = ?
168-
return ++(*val);
149+
void unwind() const override {
150+
if (val) delete val;
169151
}
170152
};
171-
// release build optimizasyonunu ekarte etmek icin
172153
if (should_throw % 2 == 0)
173-
throw unwinder_impl(x.get());
154+
throw stack_cleaner(val);
155+
else
156+
delete val;
174157
}
175158

176159
void usage()
177160
{
178161
try
179162
{
180-
will_throw(rand());
163+
while (true)
164+
{
165+
will_throw(rand());
166+
}
181167
}
182-
catch (const unwinder& _unwinder)
168+
catch (const unwinder& unwinder)
183169
{
184-
int res = _unwinder.unwind();
185-
std::cout << "result: " << res << "\n";
170+
unwinder.unwind();
186171
}
187172
}
188173

@@ -196,3 +181,8 @@ ELEMENT_CODE(ExceptionUsage) {
196181
rethrow::usage();
197182
unwinding::usage();
198183
}
184+
185+
ELEMENT_CODE(ExceptionUnwinding) {
186+
using namespace exceptions;
187+
unwinding::usage();
188+
}

0 commit comments

Comments
 (0)