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