-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracebacks.cpp
More file actions
40 lines (35 loc) · 748 Bytes
/
tracebacks.cpp
File metadata and controls
40 lines (35 loc) · 748 Bytes
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
#include <bits/stdc++.h>
using namespace std;
#define decorate(func) _decorate(string(#func), func)
class exc {
public:
string what_arg;
explicit exc ( string wht) {
what_arg = wht;
}
string what() {
return what_arg;
}
};
template<typename R, typename ... Args> function<R(Args...)> _decorate(string name, R (*f) (Args...) ) {
return function<R(Args...)>([=](Args ... args) {
try {
return f(args...);
}
catch (exc e) {
throw exc("In " + name + ":\n" + e.what());
}
});
}
void test(int depth) {
if(depth > 10) throw exc("Maximum recursion depth exceeded.");
decorate(test)(depth+1);
}
int main() {
try {
decorate(test)(0);
}
catch (exc e) {
cerr << e.what() << endl;
}
}