[6d43cdde] | 1 | // Resumption Exception Tests
|
---|
| 2 |
|
---|
| 3 | #include "except-io.hfa"
|
---|
| 4 |
|
---|
[d00d581] | 5 | exception yin {};
|
---|
| 6 | exception yang {};
|
---|
| 7 | exception zen {};
|
---|
[ecfd758] | 8 |
|
---|
[d00d581] | 9 | vtable(yin) yin_vt;
|
---|
| 10 | vtable(yang) yang_vt;
|
---|
| 11 | vtable(zen) zen_vt;
|
---|
[6d43cdde] | 12 |
|
---|
[7f9968ad] | 13 | void in_void(void);
|
---|
| 14 |
|
---|
[6d43cdde] | 15 | int main(int argc, char * argv[]) {
|
---|
[ecfd758] | 16 | yin a_yin = {&yin_vt};
|
---|
| 17 | yang a_yang = {&yang_vt};
|
---|
| 18 | zen a_zen = {&zen_vt};
|
---|
| 19 |
|
---|
[6d43cdde] | 20 | // The simple throw catchResume test.
|
---|
| 21 | try {
|
---|
| 22 | loud_exit a = "simple try clause";
|
---|
| 23 | printf("simple throw\n");
|
---|
[ecfd758] | 24 | throwResume a_zen;
|
---|
[6d43cdde] | 25 | printf("end of try clause\n");
|
---|
| 26 | } catchResume (zen * error) {
|
---|
| 27 | loud_exit a = "simple catch clause";
|
---|
| 28 | printf("simple catch\n");
|
---|
| 29 | }
|
---|
| 30 | printf("\n");
|
---|
| 31 |
|
---|
[fe1025d] | 32 | // Throw catch-all test.
|
---|
| 33 | try {
|
---|
[ecfd758] | 34 | throwResume a_zen;
|
---|
[fe1025d] | 35 | } catchResume (exception_t * error) {
|
---|
| 36 | printf("catch-all\n");
|
---|
| 37 | }
|
---|
| 38 | printf("\n");
|
---|
| 39 |
|
---|
[6d43cdde] | 40 | // Don't catch if handler does not match exception.
|
---|
| 41 | try {
|
---|
| 42 | try {
|
---|
[ecfd758] | 43 | throwResume a_yin;
|
---|
[6d43cdde] | 44 | } catchResume (zen *) {
|
---|
| 45 | printf("caught yin as zen\n");
|
---|
| 46 | }
|
---|
| 47 | } catchResume (yang *) {
|
---|
| 48 | printf("caught yin as yang\n");
|
---|
| 49 | } catchResume (yin *) {
|
---|
| 50 | printf("caught yin as yin\n");
|
---|
| 51 | }
|
---|
| 52 | printf("\n");
|
---|
| 53 |
|
---|
| 54 | // Test rethrowing an exception.
|
---|
| 55 | try {
|
---|
| 56 | try {
|
---|
| 57 | loud_exit a = "rethrow inner try";
|
---|
| 58 | printf("rethrow inner try\n");
|
---|
[ecfd758] | 59 | throwResume a_zen;
|
---|
[6d43cdde] | 60 | } catchResume (zen *) {
|
---|
| 61 | loud_exit a = "rethrowing catch clause";
|
---|
| 62 | printf("caught throw, will rethrow\n");
|
---|
| 63 | throwResume;
|
---|
| 64 | }
|
---|
| 65 | } catchResume (zen *) {
|
---|
| 66 | loud_exit a = "rethrow catch clause";
|
---|
| 67 | printf("caught rethrow\n");
|
---|
| 68 | }
|
---|
| 69 | printf("\n");
|
---|
| 70 |
|
---|
| 71 | // Throw a different exception in a catch.
|
---|
| 72 | try {
|
---|
| 73 | try {
|
---|
[ecfd758] | 74 | throwResume a_yin;
|
---|
[6d43cdde] | 75 | } catchResume (yin *) {
|
---|
| 76 | printf("caught yin, will throw yang\n");
|
---|
[ecfd758] | 77 | throwResume a_yang;
|
---|
[6d43cdde] | 78 | } catchResume (yang *) {
|
---|
| 79 | printf("caught exception from same try\n");
|
---|
| 80 | }
|
---|
| 81 | } catchResume (yang *) {
|
---|
| 82 | printf("caught yang\n");
|
---|
| 83 | }
|
---|
| 84 | printf("\n");
|
---|
| 85 |
|
---|
| 86 | // Another throw in the catch does not interfere.
|
---|
| 87 | try {
|
---|
| 88 | try {
|
---|
| 89 | printf("throwing first exception\n");
|
---|
[ecfd758] | 90 | throwResume a_yin;
|
---|
[6d43cdde] | 91 | } catchResume (yin *) {
|
---|
| 92 | printf("caught first exception\n");
|
---|
| 93 | try {
|
---|
| 94 | printf("throwing second exception\n");
|
---|
[ecfd758] | 95 | throwResume a_yang;
|
---|
[6d43cdde] | 96 | } catchResume (yang *) {
|
---|
| 97 | printf("caught second exception\n");
|
---|
| 98 | }
|
---|
| 99 | throwResume;
|
---|
| 100 | }
|
---|
| 101 | } catchResume (yin *) {
|
---|
| 102 | printf("recaught first exception\n");
|
---|
| 103 | } catchResume (yang *) {
|
---|
| 104 | printf("caught second exception (bad location)\n");
|
---|
| 105 | }
|
---|
[f1b6671] | 106 | printf("\n");
|
---|
| 107 |
|
---|
| 108 | // Check successive operations.
|
---|
| 109 | try {
|
---|
| 110 | try {
|
---|
[ecfd758] | 111 | throwResume a_zen;
|
---|
| 112 | throwResume a_zen;
|
---|
[f1b6671] | 113 | } catchResume (zen *) {
|
---|
| 114 | printf("inner catch\n");
|
---|
| 115 | }
|
---|
[ecfd758] | 116 | throwResume a_zen;
|
---|
[f1b6671] | 117 | } catchResume (zen *) {
|
---|
| 118 | printf("outer catch\n");
|
---|
| 119 | }
|
---|
[7f9968ad] | 120 | printf("\n");
|
---|
| 121 |
|
---|
| 122 | in_void();
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | // Do a throw and rethrow in a void function.
|
---|
| 126 | void in_void(void) {
|
---|
[ecfd758] | 127 | zen a_zen = {&zen_vt};
|
---|
[7f9968ad] | 128 | try {
|
---|
| 129 | try {
|
---|
| 130 | printf("throw\n");
|
---|
[ecfd758] | 131 | throwResume a_zen;
|
---|
[7f9968ad] | 132 | } catchResume (zen *) {
|
---|
| 133 | printf("rethrow\n");
|
---|
| 134 | throwResume;
|
---|
| 135 | }
|
---|
| 136 | } catchResume (zen *) {
|
---|
| 137 | printf("handle\n");
|
---|
| 138 | }
|
---|
[6d43cdde] | 139 | }
|
---|