source: tests/exceptions/resume.cfa @ a7efc96

Last change on this file since a7efc96 was 3bf9d10, checked in by Peter A. Buhr <pabuhr@…>, 17 months ago

change printf to sout

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