[6d43cdde] | 1 | // Termination Exception Tests |
---|
| 2 | |
---|
| 3 | #include "except-io.hfa" |
---|
| 4 | |
---|
[d00d581] | 5 | exception yin {}; |
---|
| 6 | exception yang {}; |
---|
| 7 | exception zen {}; |
---|
[ecfd758] | 8 | |
---|
[d00d581] | 9 | vtable(yin) yin_vt; |
---|
| 10 | vtable(yang) yang_vt; |
---|
| 11 | vtable(zen) zen_vt; |
---|
[6d43cdde] | 12 | |
---|
[7f9968a] | 13 | void in_void(void); |
---|
| 14 | |
---|
[6d43cdde] | 15 | int main(int argc, char * argv[]) { |
---|
[ecfd758] | 16 | yin a_yin = {&yin_vt}; |
---|
| 17 | yang a_yang = {&yang_vt}; |
---|
| 18 | zen a_zen = {&zen_vt}; |
---|
| 19 | |
---|
[6d43cdde] | 20 | // The simple throw catch test. |
---|
| 21 | try { |
---|
| 22 | loud_exit a = "simple try clause"; |
---|
| 23 | printf("simple throw\n"); |
---|
[ecfd758] | 24 | throw a_zen; |
---|
[6d43cdde] | 25 | printf("end of try clause\n"); |
---|
| 26 | } catch (zen * error) { |
---|
[fe1025d] | 27 | loud_exit a = "simple catch clause"; |
---|
[6d43cdde] | 28 | printf("simple catch\n"); |
---|
| 29 | } |
---|
| 30 | printf("\n"); |
---|
| 31 | |
---|
[fe1025d] | 32 | // Throw catch-all test. |
---|
| 33 | try { |
---|
[ecfd758] | 34 | throw a_zen; |
---|
[fe1025d] | 35 | } catch (exception_t * error) { |
---|
| 36 | printf("catch-all\n"); |
---|
| 37 | } |
---|
| 38 | printf("\n"); |
---|
| 39 | |
---|
[6d43cdde] | 40 | // Don't catch if handler does not match exception. |
---|
| 41 | try { |
---|
| 42 | try { |
---|
[ecfd758] | 43 | throw a_yin; |
---|
[6d43cdde] | 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"); |
---|
[ecfd758] | 59 | throw a_zen; |
---|
[6d43cdde] | 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 { |
---|
[ecfd758] | 74 | throw a_yin; |
---|
[6d43cdde] | 75 | } catch (yin *) { |
---|
| 76 | printf("caught yin, will throw yang\n"); |
---|
[ecfd758] | 77 | throw a_yang; |
---|
[6d43cdde] | 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"); |
---|
[ecfd758] | 90 | throw a_yin; |
---|
[6d43cdde] | 91 | } catch (yin *) { |
---|
| 92 | printf("caught first exception\n"); |
---|
| 93 | try { |
---|
| 94 | printf("throwing second exception\n"); |
---|
[ecfd758] | 95 | throw a_yang; |
---|
[6d43cdde] | 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 | } |
---|
[f1b6671] | 106 | printf("\n"); |
---|
| 107 | |
---|
| 108 | // Check successive operations. |
---|
| 109 | try { |
---|
| 110 | try { |
---|
[ecfd758] | 111 | throw a_zen; |
---|
| 112 | throw a_zen; |
---|
[f1b6671] | 113 | } catch (zen *) { |
---|
| 114 | printf("inner catch\n"); |
---|
| 115 | } |
---|
[ecfd758] | 116 | throw a_zen; |
---|
[f1b6671] | 117 | } catch (zen *) { |
---|
| 118 | printf("outer catch\n"); |
---|
| 119 | } |
---|
[7f9968a] | 120 | printf("\n"); |
---|
| 121 | |
---|
| 122 | in_void(); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | // Do a throw and rethrow in a void function. |
---|
| 126 | void in_void(void) { |
---|
[ecfd758] | 127 | zen a_zen = {&zen_vt}; |
---|
[7f9968a] | 128 | try { |
---|
| 129 | try { |
---|
| 130 | printf("throw\n"); |
---|
[ecfd758] | 131 | throw a_zen; |
---|
[7f9968a] | 132 | } catch (zen *) { |
---|
| 133 | printf("rethrow\n"); |
---|
| 134 | throw; |
---|
| 135 | } |
---|
| 136 | } catch (zen *) { |
---|
| 137 | printf("handle\n"); |
---|
| 138 | } |
---|
[6d43cdde] | 139 | } |
---|