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