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