[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 |
|
---|
| 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");
|
---|
[e68d092] | 16 | throw &(zen){};
|
---|
[6d43cdde] | 17 | printf("end of try clause\n");
|
---|
| 18 | } catch (zen * error) {
|
---|
[fe1025d] | 19 | loud_exit a = "simple catch clause";
|
---|
[6d43cdde] | 20 | printf("simple catch\n");
|
---|
| 21 | }
|
---|
| 22 | printf("\n");
|
---|
| 23 |
|
---|
[fe1025d] | 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 |
|
---|
[6d43cdde] | 32 | // Catch a parent of the given exception.
|
---|
| 33 | try {
|
---|
| 34 | printf("throwing child exception\n");
|
---|
[e68d092] | 35 | throw &(moment_of){};
|
---|
[6d43cdde] | 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 {
|
---|
[e68d092] | 46 | throw &(yin){};
|
---|
[6d43cdde] | 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");
|
---|
[e68d092] | 62 | throw &(zen){};
|
---|
[6d43cdde] | 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 {
|
---|
[e68d092] | 77 | throw &(yin){};
|
---|
[6d43cdde] | 78 | } catch (yin *) {
|
---|
| 79 | printf("caught yin, will throw yang\n");
|
---|
[e68d092] | 80 | throw &(yang){};
|
---|
[6d43cdde] | 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");
|
---|
[e68d092] | 93 | throw &(yin){};
|
---|
[6d43cdde] | 94 | } catch (yin *) {
|
---|
| 95 | printf("caught first exception\n");
|
---|
| 96 | try {
|
---|
| 97 | printf("throwing second exception\n");
|
---|
[e68d092] | 98 | throw &(yang){};
|
---|
[6d43cdde] | 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 | }
|
---|
[f1b6671] | 109 | printf("\n");
|
---|
| 110 |
|
---|
| 111 | // Check successive operations.
|
---|
| 112 | try {
|
---|
| 113 | try {
|
---|
[e68d092] | 114 | throw &(zen){};
|
---|
| 115 | throw &(zen){};
|
---|
[f1b6671] | 116 | } catch (zen *) {
|
---|
| 117 | printf("inner catch\n");
|
---|
| 118 | }
|
---|
[e68d092] | 119 | throw &(zen){};
|
---|
[f1b6671] | 120 | } catch (zen *) {
|
---|
| 121 | printf("outer catch\n");
|
---|
| 122 | }
|
---|
[6d43cdde] | 123 | }
|
---|