| 1 | // Resumption 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 catchResume test. | 
|---|
| 21 | try { | 
|---|
| 22 | loud_exit a = "simple try clause"; | 
|---|
| 23 | printf("simple throw\n"); | 
|---|
| 24 | throwResume a_zen; | 
|---|
| 25 | printf("end of try clause\n"); | 
|---|
| 26 | } catchResume (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 | throwResume a_zen; | 
|---|
| 35 | } catchResume (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 | throwResume a_yin; | 
|---|
| 44 | } catchResume (zen *) { | 
|---|
| 45 | printf("caught yin as zen\n"); | 
|---|
| 46 | } | 
|---|
| 47 | } catchResume (yang *) { | 
|---|
| 48 | printf("caught yin as yang\n"); | 
|---|
| 49 | } catchResume (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 | throwResume a_zen; | 
|---|
| 60 | } catchResume (zen *) { | 
|---|
| 61 | loud_exit a = "rethrowing catch clause"; | 
|---|
| 62 | printf("caught throw, will rethrow\n"); | 
|---|
| 63 | throwResume; | 
|---|
| 64 | } | 
|---|
| 65 | } catchResume (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 | throwResume a_yin; | 
|---|
| 75 | } catchResume (yin *) { | 
|---|
| 76 | printf("caught yin, will throw yang\n"); | 
|---|
| 77 | throwResume a_yang; | 
|---|
| 78 | } catchResume (yang *) { | 
|---|
| 79 | printf("caught exception from same try\n"); | 
|---|
| 80 | } | 
|---|
| 81 | } catchResume (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 | throwResume a_yin; | 
|---|
| 91 | } catchResume (yin *) { | 
|---|
| 92 | printf("caught first exception\n"); | 
|---|
| 93 | try { | 
|---|
| 94 | printf("throwing second exception\n"); | 
|---|
| 95 | throwResume a_yang; | 
|---|
| 96 | } catchResume (yang *) { | 
|---|
| 97 | printf("caught second exception\n"); | 
|---|
| 98 | } | 
|---|
| 99 | throwResume; | 
|---|
| 100 | } | 
|---|
| 101 | } catchResume (yin *) { | 
|---|
| 102 | printf("recaught first exception\n"); | 
|---|
| 103 | } catchResume (yang *) { | 
|---|
| 104 | printf("caught second exception (bad location)\n"); | 
|---|
| 105 | } | 
|---|
| 106 | printf("\n"); | 
|---|
| 107 |  | 
|---|
| 108 | // Check successive operations. | 
|---|
| 109 | try { | 
|---|
| 110 | try { | 
|---|
| 111 | throwResume a_zen; | 
|---|
| 112 | throwResume a_zen; | 
|---|
| 113 | } catchResume (zen *) { | 
|---|
| 114 | printf("inner catch\n"); | 
|---|
| 115 | } | 
|---|
| 116 | throwResume a_zen; | 
|---|
| 117 | } catchResume (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 | throwResume a_zen; | 
|---|
| 132 | } catchResume (zen *) { | 
|---|
| 133 | printf("rethrow\n"); | 
|---|
| 134 | throwResume; | 
|---|
| 135 | } | 
|---|
| 136 | } catchResume (zen *) { | 
|---|
| 137 | printf("handle\n"); | 
|---|
| 138 | } | 
|---|
| 139 | } | 
|---|