throw、try、catch

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
#include <stdio.h>

void handler() {
printf_s("in handler\n");
}

void f1(void) throw(int) {
printf_s("About to throw 1\n");
if (1)
throw 1;
}

void f5(void) throw() {
try {
f1();
}
catch(...) {
handler();
}
}

//invalid, doesn't handle the int exception thrown from f1()
/*void f3(void) throw() {
f1();
}*/

void __declspec(nothrow) f2(void) {
try {
f1();
}
catch(int) {
handler();
}
}

extern "C" void f4(void);
void f4(void) {
f1();
}

int main() {
f2();

try {
f4();
}
catch(...) {
printf_s("Caught exception from f4\n");
}
f5();
}