source: tests/exceptions/resume.cfa@ 381132b

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 381132b was 7f9968ad, checked in by Andrew Beach <ajbeach@…>, 5 years ago

Fixed a problem with 'throwResume;' translation and added some tests to check for similar problems.

  • Property mode set to 100644
File size: 2.9 KB
RevLine 
[6d43cdde]1// Resumption Exception Tests
2
[e68d092]3#include <exception.hfa>
[6d43cdde]4#include "except-io.hfa"
5
6TRIVIAL_EXCEPTION(yin);
7TRIVIAL_EXCEPTION(yang);
8TRIVIAL_EXCEPTION(zen);
9TRIVIAL_EXCEPTION(moment_of, zen);
10
[7f9968ad]11void in_void(void);
12
[6d43cdde]13int 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");
[046a890]18 throwResume (zen){};
[6d43cdde]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
[fe1025d]26 // Throw catch-all test.
27 try {
[046a890]28 throwResume (zen){};
[fe1025d]29 } catchResume (exception_t * error) {
30 printf("catch-all\n");
31 }
32 printf("\n");
33
[6d43cdde]34 // Catch a parent of the given exception.
35 try {
36 printf("throwing child exception\n");
[046a890]37 throwResume (moment_of){};
[6d43cdde]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 {
[046a890]48 throwResume (yin){};
[6d43cdde]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");
[046a890]64 throwResume (zen){};
[6d43cdde]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 {
[046a890]79 throwResume (yin){};
[6d43cdde]80 } catchResume (yin *) {
81 printf("caught yin, will throw yang\n");
[046a890]82 throwResume (yang){};
[6d43cdde]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");
[046a890]95 throwResume (yin){};
[6d43cdde]96 } catchResume (yin *) {
97 printf("caught first exception\n");
98 try {
99 printf("throwing second exception\n");
[046a890]100 throwResume (yang){};
[6d43cdde]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 }
[f1b6671]111 printf("\n");
112
113 // Check successive operations.
114 try {
115 try {
[046a890]116 throwResume (zen){};
117 throwResume (zen){};
[f1b6671]118 } catchResume (zen *) {
119 printf("inner catch\n");
120 }
[046a890]121 throwResume (zen){};
[f1b6671]122 } catchResume (zen *) {
123 printf("outer catch\n");
124 }
[7f9968ad]125 printf("\n");
126
127 in_void();
128}
129
130// Do a throw and rethrow in a void function.
131void 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 }
[6d43cdde]143}
Note: See TracBrowser for help on using the repository browser.