| [6d43cdde] | 1 | // Termination 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 |  | 
|---|
| [7f9968ad] | 11 | void in_void(void); | 
|---|
|  | 12 |  | 
|---|
| [6d43cdde] | 13 | int main(int argc, char * argv[]) { | 
|---|
|  | 14 | // The simple throw catch test. | 
|---|
|  | 15 | try { | 
|---|
|  | 16 | loud_exit a = "simple try clause"; | 
|---|
|  | 17 | printf("simple throw\n"); | 
|---|
| [046a890] | 18 | throw (zen){}; | 
|---|
| [6d43cdde] | 19 | printf("end of try clause\n"); | 
|---|
|  | 20 | } catch (zen * error) { | 
|---|
| [fe1025d] | 21 | loud_exit a = "simple catch clause"; | 
|---|
| [6d43cdde] | 22 | printf("simple catch\n"); | 
|---|
|  | 23 | } | 
|---|
|  | 24 | printf("\n"); | 
|---|
|  | 25 |  | 
|---|
| [fe1025d] | 26 | // Throw catch-all test. | 
|---|
|  | 27 | try { | 
|---|
| [046a890] | 28 | throw (zen){}; | 
|---|
| [fe1025d] | 29 | } catch (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 | throw (moment_of){}; | 
|---|
| [6d43cdde] | 38 | } catch (zen *) { | 
|---|
|  | 39 | printf("inner parent match\n"); | 
|---|
|  | 40 | } catch (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 | throw (yin){}; | 
|---|
| [6d43cdde] | 49 | } catch (zen *) { | 
|---|
|  | 50 | printf("caught yin as zen\n"); | 
|---|
|  | 51 | } | 
|---|
|  | 52 | } catch (yang *) { | 
|---|
|  | 53 | printf("caught yin as yang\n"); | 
|---|
|  | 54 | } catch (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 | throw (zen){}; | 
|---|
| [6d43cdde] | 65 | } catch (zen *) { | 
|---|
|  | 66 | loud_exit a = "rethrowing catch clause"; | 
|---|
|  | 67 | printf("caught throw, will rethrow\n"); | 
|---|
|  | 68 | throw; | 
|---|
|  | 69 | } | 
|---|
|  | 70 | } catch (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 | throw (yin){}; | 
|---|
| [6d43cdde] | 80 | } catch (yin *) { | 
|---|
|  | 81 | printf("caught yin, will throw yang\n"); | 
|---|
| [046a890] | 82 | throw (yang){}; | 
|---|
| [6d43cdde] | 83 | } catch (yang *) { | 
|---|
|  | 84 | printf("caught exception from same try\n"); | 
|---|
|  | 85 | } | 
|---|
|  | 86 | } catch (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 | throw (yin){}; | 
|---|
| [6d43cdde] | 96 | } catch (yin *) { | 
|---|
|  | 97 | printf("caught first exception\n"); | 
|---|
|  | 98 | try { | 
|---|
|  | 99 | printf("throwing second exception\n"); | 
|---|
| [046a890] | 100 | throw (yang){}; | 
|---|
| [6d43cdde] | 101 | } catch (yang *) { | 
|---|
|  | 102 | printf("caught second exception\n"); | 
|---|
|  | 103 | } | 
|---|
|  | 104 | throw; | 
|---|
|  | 105 | } | 
|---|
|  | 106 | } catch (yin *) { | 
|---|
|  | 107 | printf("recaught first exception\n"); | 
|---|
|  | 108 | } catch (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 | throw (zen){}; | 
|---|
|  | 117 | throw (zen){}; | 
|---|
| [f1b6671] | 118 | } catch (zen *) { | 
|---|
|  | 119 | printf("inner catch\n"); | 
|---|
|  | 120 | } | 
|---|
| [046a890] | 121 | throw (zen){}; | 
|---|
| [f1b6671] | 122 | } catch (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. | 
|---|
|  | 131 | void in_void(void) { | 
|---|
|  | 132 | try { | 
|---|
|  | 133 | try { | 
|---|
|  | 134 | printf("throw\n"); | 
|---|
|  | 135 | throw (zen){}; | 
|---|
|  | 136 | } catch (zen *) { | 
|---|
|  | 137 | printf("rethrow\n"); | 
|---|
|  | 138 | throw; | 
|---|
|  | 139 | } | 
|---|
|  | 140 | } catch (zen *) { | 
|---|
|  | 141 | printf("handle\n"); | 
|---|
|  | 142 | } | 
|---|
| [6d43cdde] | 143 | } | 
|---|