| 1 | // Termination Exception Tests
 | 
|---|
| 2 | 
 | 
|---|
| 3 | #include <exception.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 | void in_void(void);
 | 
|---|
| 12 | 
 | 
|---|
| 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");
 | 
|---|
| 18 |                 throw (zen){};
 | 
|---|
| 19 |                 printf("end of try clause\n");
 | 
|---|
| 20 |         } catch (zen * error) {
 | 
|---|
| 21 |                 loud_exit a = "simple catch clause";
 | 
|---|
| 22 |                 printf("simple catch\n");
 | 
|---|
| 23 |         }
 | 
|---|
| 24 |         printf("\n");
 | 
|---|
| 25 | 
 | 
|---|
| 26 |         // Throw catch-all test.
 | 
|---|
| 27 |         try {
 | 
|---|
| 28 |                 throw (zen){};
 | 
|---|
| 29 |         } catch (exception_t * error) {
 | 
|---|
| 30 |                 printf("catch-all\n");
 | 
|---|
| 31 |         }
 | 
|---|
| 32 |         printf("\n");
 | 
|---|
| 33 | 
 | 
|---|
| 34 |         // Catch a parent of the given exception.
 | 
|---|
| 35 |         try {
 | 
|---|
| 36 |                 printf("throwing child exception\n");
 | 
|---|
| 37 |                 throw (moment_of){};
 | 
|---|
| 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 {
 | 
|---|
| 48 |                         throw (yin){};
 | 
|---|
| 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");
 | 
|---|
| 64 |                         throw (zen){};
 | 
|---|
| 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 {
 | 
|---|
| 79 |                         throw (yin){};
 | 
|---|
| 80 |                 } catch (yin *) {
 | 
|---|
| 81 |                         printf("caught yin, will throw yang\n");
 | 
|---|
| 82 |                         throw (yang){};
 | 
|---|
| 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");
 | 
|---|
| 95 |                         throw (yin){};
 | 
|---|
| 96 |                 } catch (yin *) {
 | 
|---|
| 97 |                         printf("caught first exception\n");
 | 
|---|
| 98 |                         try {
 | 
|---|
| 99 |                                 printf("throwing second exception\n");
 | 
|---|
| 100 |                                 throw (yang){};
 | 
|---|
| 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 |         }
 | 
|---|
| 111 |         printf("\n");
 | 
|---|
| 112 | 
 | 
|---|
| 113 |         // Check successive operations.
 | 
|---|
| 114 |         try {
 | 
|---|
| 115 |                 try {
 | 
|---|
| 116 |                         throw (zen){};
 | 
|---|
| 117 |                         throw (zen){};
 | 
|---|
| 118 |                 } catch (zen *) {
 | 
|---|
| 119 |                         printf("inner catch\n");
 | 
|---|
| 120 |                 }
 | 
|---|
| 121 |                 throw (zen){};
 | 
|---|
| 122 |         } catch (zen *) {
 | 
|---|
| 123 |                 printf("outer catch\n");
 | 
|---|
| 124 |         }
 | 
|---|
| 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 |         }
 | 
|---|
| 143 | }
 | 
|---|