1 | // Resumption Exception Tests |
---|
2 | |
---|
3 | #include <exception.hfa> |
---|
4 | #include "except-io.hfa" |
---|
5 | |
---|
6 | TRIVIAL_EXCEPTION(yin); |
---|
7 | TRIVIAL_EXCEPTION(yang); |
---|
8 | TRIVIAL_EXCEPTION(zen); |
---|
9 | TRIVIAL_EXCEPTION(moment_of, zen); |
---|
10 | |
---|
11 | void in_void(void); |
---|
12 | |
---|
13 | int main(int argc, char * argv[]) { |
---|
14 | // The simple throw catchResume test. |
---|
15 | try { |
---|
16 | loud_exit a = "simple try clause"; |
---|
17 | printf("simple throw\n"); |
---|
18 | throwResume (zen){}; |
---|
19 | printf("end of try clause\n"); |
---|
20 | } catchResume (zen * error) { |
---|
21 | loud_exit a = "simple catch clause"; |
---|
22 | printf("simple catch\n"); |
---|
23 | } |
---|
24 | printf("\n"); |
---|
25 | |
---|
26 | // Throw catch-all test. |
---|
27 | try { |
---|
28 | throwResume (zen){}; |
---|
29 | } catchResume (exception_t * error) { |
---|
30 | printf("catch-all\n"); |
---|
31 | } |
---|
32 | printf("\n"); |
---|
33 | |
---|
34 | // Catch a parent of the given exception. |
---|
35 | try { |
---|
36 | printf("throwing child exception\n"); |
---|
37 | throwResume (moment_of){}; |
---|
38 | } catchResume (zen *) { |
---|
39 | printf("inner parent match\n"); |
---|
40 | } catchResume (moment_of *) { |
---|
41 | printf("outer exact match\n"); |
---|
42 | } |
---|
43 | printf("\n"); |
---|
44 | |
---|
45 | // Don't catch if handler does not match exception. |
---|
46 | try { |
---|
47 | try { |
---|
48 | throwResume (yin){}; |
---|
49 | } catchResume (zen *) { |
---|
50 | printf("caught yin as zen\n"); |
---|
51 | } |
---|
52 | } catchResume (yang *) { |
---|
53 | printf("caught yin as yang\n"); |
---|
54 | } catchResume (yin *) { |
---|
55 | printf("caught yin as yin\n"); |
---|
56 | } |
---|
57 | printf("\n"); |
---|
58 | |
---|
59 | // Test rethrowing an exception. |
---|
60 | try { |
---|
61 | try { |
---|
62 | loud_exit a = "rethrow inner try"; |
---|
63 | printf("rethrow inner try\n"); |
---|
64 | throwResume (zen){}; |
---|
65 | } catchResume (zen *) { |
---|
66 | loud_exit a = "rethrowing catch clause"; |
---|
67 | printf("caught throw, will rethrow\n"); |
---|
68 | throwResume; |
---|
69 | } |
---|
70 | } catchResume (zen *) { |
---|
71 | loud_exit a = "rethrow catch clause"; |
---|
72 | printf("caught rethrow\n"); |
---|
73 | } |
---|
74 | printf("\n"); |
---|
75 | |
---|
76 | // Throw a different exception in a catch. |
---|
77 | try { |
---|
78 | try { |
---|
79 | throwResume (yin){}; |
---|
80 | } catchResume (yin *) { |
---|
81 | printf("caught yin, will throw yang\n"); |
---|
82 | throwResume (yang){}; |
---|
83 | } catchResume (yang *) { |
---|
84 | printf("caught exception from same try\n"); |
---|
85 | } |
---|
86 | } catchResume (yang *) { |
---|
87 | printf("caught yang\n"); |
---|
88 | } |
---|
89 | printf("\n"); |
---|
90 | |
---|
91 | // Another throw in the catch does not interfere. |
---|
92 | try { |
---|
93 | try { |
---|
94 | printf("throwing first exception\n"); |
---|
95 | throwResume (yin){}; |
---|
96 | } catchResume (yin *) { |
---|
97 | printf("caught first exception\n"); |
---|
98 | try { |
---|
99 | printf("throwing second exception\n"); |
---|
100 | throwResume (yang){}; |
---|
101 | } catchResume (yang *) { |
---|
102 | printf("caught second exception\n"); |
---|
103 | } |
---|
104 | throwResume; |
---|
105 | } |
---|
106 | } catchResume (yin *) { |
---|
107 | printf("recaught first exception\n"); |
---|
108 | } catchResume (yang *) { |
---|
109 | printf("caught second exception (bad location)\n"); |
---|
110 | } |
---|
111 | printf("\n"); |
---|
112 | |
---|
113 | // Check successive operations. |
---|
114 | try { |
---|
115 | try { |
---|
116 | throwResume (zen){}; |
---|
117 | throwResume (zen){}; |
---|
118 | } catchResume (zen *) { |
---|
119 | printf("inner catch\n"); |
---|
120 | } |
---|
121 | throwResume (zen){}; |
---|
122 | } catchResume (zen *) { |
---|
123 | printf("outer catch\n"); |
---|
124 | } |
---|
125 | printf("\n"); |
---|
126 | |
---|
127 | in_void(); |
---|
128 | } |
---|
129 | |
---|
130 | // Do a throw and rethrow in a void function. |
---|
131 | void in_void(void) { |
---|
132 | try { |
---|
133 | try { |
---|
134 | printf("throw\n"); |
---|
135 | throwResume (zen){}; |
---|
136 | } catchResume (zen *) { |
---|
137 | printf("rethrow\n"); |
---|
138 | throwResume; |
---|
139 | } |
---|
140 | } catchResume (zen *) { |
---|
141 | printf("handle\n"); |
---|
142 | } |
---|
143 | } |
---|