-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrycatch.c
More file actions
37 lines (31 loc) · 890 Bytes
/
Copy pathtrycatch.c
File metadata and controls
37 lines (31 loc) · 890 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
#include <pthread.h>
#include <setjmp.h>
#include <stdio.h>
typedef struct _Exception {
jmp_buf env;
int exceptype;
} Exception;
Exception *header;
#define Try(excep) if ((excep.exceptype = setjmp(excep.env)) == 0)
#define Catch(excep, ExceptionType) else if (excep.exceptype == ExceptionType)
#define Throw(excep, ExceptionType) longjmp(excep.env, ExceptionType)
#define Finally
void throw_func(Exception excep, int idx) {
printf("throw_func --> idx :%d\n", idx);
Throw(excep, idx);
}
int main() {
Exception excep;
excep.exceptype = 0;
Try(excep) {
printf("count: %d\n", excep.exceptype);
throw_func(excep, 2);
}
Catch(excep, 1) { printf("count: %d\n", excep.exceptype); }
Catch(excep, 2) {
printf("count: %d\n", excep.exceptype);
throw_func(excep, 1);
}
Finally { printf("Finally\n"); }
return 0;
}