| 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 | 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 |         // Throw catch-all test.
 | 
|---|
| 25 |         try {
 | 
|---|
| 26 |                 throw (zen){};
 | 
|---|
| 27 |         } catch (exception_t * error) {
 | 
|---|
| 28 |                 printf("catch-all\n");
 | 
|---|
| 29 |         }
 | 
|---|
| 30 |         printf("\n");
 | 
|---|
| 31 | 
 | 
|---|
| 32 |         // Catch a parent of the given exception.
 | 
|---|
| 33 |         try {
 | 
|---|
| 34 |                 printf("throwing child exception\n");
 | 
|---|
| 35 |                 throw (moment_of){};
 | 
|---|
| 36 |         } catch (zen *) {
 | 
|---|
| 37 |                 printf("inner parent match\n");
 | 
|---|
| 38 |         } catch (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 {
 | 
|---|
| 46 |                         throw (yin){};
 | 
|---|
| 47 |                 } catch (zen *) {
 | 
|---|
| 48 |                         printf("caught yin as zen\n");
 | 
|---|
| 49 |                 }
 | 
|---|
| 50 |         } catch (yang *) {
 | 
|---|
| 51 |                 printf("caught yin as yang\n");
 | 
|---|
| 52 |         } catch (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");
 | 
|---|
| 62 |                         throw (zen){};
 | 
|---|
| 63 |                 } catch (zen *) {
 | 
|---|
| 64 |                         loud_exit a = "rethrowing catch clause";
 | 
|---|
| 65 |                         printf("caught throw, will rethrow\n");
 | 
|---|
| 66 |                         throw;
 | 
|---|
| 67 |                 }
 | 
|---|
| 68 |         } catch (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 {
 | 
|---|
| 77 |                         throw (yin){};
 | 
|---|
| 78 |                 } catch (yin *) {
 | 
|---|
| 79 |                         printf("caught yin, will throw yang\n");
 | 
|---|
| 80 |                         throw (yang){};
 | 
|---|
| 81 |                 } catch (yang *) {
 | 
|---|
| 82 |                         printf("caught exception from same try\n");
 | 
|---|
| 83 |                 }
 | 
|---|
| 84 |         } catch (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");
 | 
|---|
| 93 |                         throw (yin){};
 | 
|---|
| 94 |                 } catch (yin *) {
 | 
|---|
| 95 |                         printf("caught first exception\n");
 | 
|---|
| 96 |                         try {
 | 
|---|
| 97 |                                 printf("throwing second exception\n");
 | 
|---|
| 98 |                                 throw (yang){};
 | 
|---|
| 99 |                         } catch (yang *) {
 | 
|---|
| 100 |                                 printf("caught second exception\n");
 | 
|---|
| 101 |                         }
 | 
|---|
| 102 |                         throw;
 | 
|---|
| 103 |                 }
 | 
|---|
| 104 |         } catch (yin *) {
 | 
|---|
| 105 |                 printf("recaught first exception\n");
 | 
|---|
| 106 |         } catch (yang *) {
 | 
|---|
| 107 |                 printf("caught second exception (bad location)\n");
 | 
|---|
| 108 |         }
 | 
|---|
| 109 |         printf("\n");
 | 
|---|
| 110 | 
 | 
|---|
| 111 |         // Check successive operations.
 | 
|---|
| 112 |         try {
 | 
|---|
| 113 |                 try {
 | 
|---|
| 114 |                         throw (zen){};
 | 
|---|
| 115 |                         throw (zen){};
 | 
|---|
| 116 |                 } catch (zen *) {
 | 
|---|
| 117 |                         printf("inner catch\n");
 | 
|---|
| 118 |                 }
 | 
|---|
| 119 |                 throw (zen){};
 | 
|---|
| 120 |         } catch (zen *) {
 | 
|---|
| 121 |                 printf("outer catch\n");
 | 
|---|
| 122 |         }
 | 
|---|
| 123 | }
 | 
|---|