-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
46 lines (36 loc) · 816 Bytes
/
main.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
#include <setjmp.h>
#include <signal.h>
#include <iostream>
jmp_buf here;
struct TestRAII {
TestRAII(int name): name(name) {
std::cout << __PRETTY_FUNCTION__ << " " << name << std::endl;
}
virtual ~TestRAII() {
std::cout << __PRETTY_FUNCTION__ << " " << name << std::endl;
}
int name;
};
void hardFail() {
TestRAII testRAII(2);
*(static_cast<int*>(0)) = 0;
}
void sighandler(int signal) {
longjmp(here, signal);
}
int main() {
try {
TestRAII testRAII(1);
signal(SIGSEGV, sighandler);
if (int err = setjmp(here)) {
char buffer[64];
snprintf(buffer, sizeof(buffer), "signal handler %d", err);
throw std::runtime_error(buffer);
}
std::cout << "Hello, World!" << std::endl;
hardFail();
} catch (std::exception const& e) {
std::cerr << e.what() << std::endl;
}
return 0;
}