forked from takanuva/ANSI-ISO-C-Exception-Handling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
167 lines (165 loc) · 2.38 KB
/
test.c
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <stdlib.h>
#include <stdio.h>
#include "exception.h"
void test0(void) {
static _Bool x = 1;
try {
if(x) {
throw(42);
} else {
printf("C");
};
printf("B");
} catch(42) {
printf("A");
x = 0;
retry;
};
};
void test1(void) {
try {
printf("D");
throw(1);
printf("E");
}
catch(1) comeback;
finally printf("F");
};
void test2(void) {
FILE *x;
try {
check x = fopen("foo.txt", "r");
printf("H");
} catch(2) {
printf("G\n");
comeback;
};
};
void test3(void) {
try {
printf("I");
try {
printf("J");
throw(42);
printf("L");
throw(51);
} catch(51) {
printf("M");
throw(666);
};
} catch(42) {
printf("K");
comeback;
} rescue {
printf("N\n");
} finally {
printf("O");
};
};
void test4(void) {
struct foo {
const char *bar;
int baz;
}
a = {"P", 10},
b = {"Q", 20},
c = {"R", 30};
try {
throw(&a);
} catch(&b) {
printf("%s", b.bar);
throw(&c);
} catch(&a) {
printf("%s", a.bar);
throw(&b);
} rescue {
struct foo *err = (struct foo *)errno;
printf("%s", err->bar);
};
};
void test5(void) {
try {
throw(20);
} catch(10) {
printf("T");
} rescue {
printf("S");
throw(10);
};
};
void test6(void) {
auto void recursion(int a) {
if(a == 5) {
printf("V");
} else {
throw(a * 10);
recursion(a + 1);
};
throw(recursion);
};
try {
recursion(0);
} catch(40) {
printf("U\n");
comeback;
} catch(recursion) {
comeback;
} rescue {
comeback;
};
};
void test7(void) {
try {
try {
throw(10);
} finally {
printf("X");
};
} catch(10) {
printf("W");
} finally printf("Y");
};
void test8(void) {
try {
const char *a = "3\n";
try {
a = "2";
throw(10);
printf("%s", a);
} finally {
printf("%s", a);
};
} catch(10) {
printf("1");
comeback;
};
};
void test9(void) {
try {
const char *a = "5";
try {
a = "foo";
throw("foo");
} catch("bar") {
printf("%s\n", a);
};
} catch("foo") {
printf("4");
throw("bar");
};
};
int main(int argc, char **argv) {
try {
test0();
test1();
test2();
test3();
test4();
test5();
test6();
test7();
test8();
test9();
};
return EXIT_SUCCESS;
};