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