Debian Codegraph @

terps/tads/tads3/test/data/catch.t

raw
1 /*
2 * test of catching a run-time exception
3 */
4
5 #include "tads.h"
6 #include "t3.h"
7
8 function _say_embed(str)
9 {
10 tadsSay(str);
11 }
12
13 function _main(args)
14 {
15 local ret;
16
17 t3SetSay(_say_embed);
18
19 test_catch_1();
20 test_catch_2();
21
22 ret = test_catch_3(nil);
23 "back in _main: test_catch_3(nil) = <<ret>>\n";
24
25 ret = test_catch_3(true);
26 "back in _main: test_catch_3(true) = <<ret>>\n";
27 }
28
29 class Throwable: object
30 // basic exception class
31 display = "basic exception object";
32 ;
33
34 export RuntimeError;
35 export exceptionMessage;
36
37 class RuntimeError: Throwable
38 construct(errno, ...) { errno_ = errno; }
39 errno_ = 0
40 exceptionMessage = nil
41 display = "RuntimeError: error = <<errno_>>"
42 ;
43
44 class Exception1: Throwable
45 construct(msg) { msg_ = msg; }
46 msg_ = ''
47 display = "This is an Exception1 with message \"<<msg_>>\""
48 ;
49
50 function test_catch_1()
51 {
52 "test catch 1 - catch our own throw\n";
53 try
54 {
55 "about to throw our error...\n";
56 throw new Exception1('hello!');
57
58 "??? can't be here\n";
59 }
60 catch (Exception1 ex)
61 {
62 "test catch 1 - caught Exception1: << ex.display >>\n";
63 }
64 catch (Throwable th)
65 {
66 "test catch 1 - caught Throwable: << th.display >>\n";
67 }
68 finally
69 {
70 "test catch 1 - in finally\n";
71 }
72 }
73
74 function test_catch_2()
75 {
76 "test catch 2 - catching a VM error\n";
77
78 try
79 {
80 local i;
81
82 "about to generate an error...\n";
83 i = 1;
84 i = i + 'x';
85
86 "??? shouldn't get here!";
87 }
88 catch (Throwable th)
89 {
90 "test catch 2 - caught Throwable: << th.display >>\n";
91 }
92 finally
93 {
94 "test catch 2 - in finally\n";
95 }
96 }
97
98 function test_catch_3(do_err)
99 {
100 "test catch 3 - catching an error while computing a return value\n";
101
102 try
103 {
104 "calculating our return value...\n";
105 return maybe_gen_err(do_err);
106 }
107 catch (Throwable th)
108 {
109 "test catch 3 - caught Throwable: << th.display >>\n";
110 "returning 'error'\n";
111 return 'error';
112 }
113 }
114
115 function maybe_gen_err(do_err)
116 {
117 "... in maybe_gen_err\n";
118 if (do_err)
119 {
120 local i;
121
122 "... causing a run-time error\n";
123 i = 0;
124 i = 1/i;
125 }
126
127 "... returning 'okay'\n";
128 return 'okay';
129 }
130