cfad47cfa3/t3compiler/tads3/test/test_err.cpp

4b825dc642cb6eb9a060e54bf8d69288fbee4904cfad47cfa334b206c65f22086bcc5d63e6f70944
1
#ifdef RCSID
2
static char RCSid[] =
3
"$Header: d:/cvsroot/tads/tads3/test/test_err.cpp,v 1.2 1999/05/17 02:52:31 MJRoberts Exp $";
4
#endif
5
6
/* 
7
 *   Copyright (c) 1998, 2002 Michael J. Roberts.  All Rights Reserved.
8
 *   
9
 *   Please see the accompanying license file, LICENSE.TXT, for information
10
 *   on using and copying this software.  
11
 */
12
/*
13
Name
14
  test_err.cpp - test of VM error handling
15
Function
16
  
17
Notes
18
  
19
Modified
20
  10/20/98 MJRoberts  - Creation
21
*/
22
23
#include <stdio.h>
24
#include "vmerr.h"
25
#include "t3test.h"
26
27
void tryItOut()
28
{
29
    static int num = 1;
30
    
31
    printf("tryItOut - throwing exception '%d'...\n", num);
32
    err_throw_a(1, 1, ERR_TYPE_INT, num++);
33
}
34
35
void wrapItUp()
36
{
37
    printf("wrapItUp\n");
38
}
39
40
void showIt(CVmException *exc)
41
{
42
    printf("error code = %d, int0 = %d\n",
43
           exc->get_error_code(), exc->get_param_int(0));
44
}
45
46
void handleIt(CVmException *exc)
47
{
48
    printf("handleIt - ");
49
    showIt(exc);
50
}
51
52
void f1()
53
{
54
    printf("f1: try, throw, finally - entering\n");
55
    err_try
56
    {
57
        tryItOut();
58
    }
59
    err_finally
60
    {
61
        wrapItUp();
62
    }
63
    err_end;
64
    printf("f1 - exiting\n");
65
}
66
67
void f2()
68
{
69
    printf("f2: try, throw, catch - entering\n");
70
    err_try
71
    {
72
        tryItOut();
73
    }
74
    err_catch(exc)
75
    {
76
        handleIt(exc);
77
    }
78
    err_end;
79
    printf("f2 - exiting\n");
80
}
81
82
void f3()
83
{
84
    printf("f3: try, throw, catch, finally - entering\n");
85
    err_try
86
    {
87
        tryItOut();
88
    }
89
    err_catch(exc)
90
    {
91
        handleIt(exc);
92
    }
93
    err_finally
94
    {
95
        wrapItUp();
96
    }
97
    err_end;
98
    printf("f3 - exiting\n");
99
}
100
101
void f4()
102
{
103
    printf("f4: try, catch - entering\n");
104
    err_try
105
    {
106
        printf("(not throwing anything)\n");
107
    }
108
    err_catch(exc)
109
    {
110
        handleIt(exc);
111
    }
112
    err_end;
113
    printf("f4 - exiting\n");
114
}
115
116
void f5()
117
{
118
    printf("f5: try, catch, finally - entering\n");
119
    err_try
120
    {
121
        printf("(not throwing anything)\n");
122
    }
123
    err_catch(exc)
124
    {
125
        handleIt(exc);
126
    }
127
    err_finally
128
    {
129
        wrapItUp();
130
    }
131
    err_end;
132
    printf("f5 - exiting\n");
133
}
134
135
void f6()
136
{
137
    printf("f6: try, throw, catch, throw - entering\n");
138
    err_try
139
    {
140
        tryItOut();
141
    }
142
    err_catch(exc)
143
    {
144
        handleIt(exc);
145
        tryItOut();
146
    }
147
    err_end;
148
    printf("f6 - exiting\n");
149
}
150
151
void f7()
152
{
153
    printf("f7: try, throw, catch, throw, finally - entering\n");
154
    err_try
155
    {
156
        tryItOut();
157
    }
158
    err_catch(exc)
159
    {
160
        handleIt(exc);
161
        tryItOut();
162
    }
163
    err_finally
164
    {
165
        printf("(in finally - not throwing anything)\n");
166
    }
167
    err_end;
168
    printf("f7 - exiting\n");
169
}
170
171
void f8()
172
{
173
    printf("f8: try, throw, catch, throw, finally, throw - entering\n");
174
    err_try
175
    {
176
        tryItOut();
177
    }
178
    err_catch(exc)
179
    {
180
        handleIt(exc);
181
        tryItOut();
182
    }
183
    err_finally
184
    {
185
        printf("in finally - throwing one more time\n");
186
        tryItOut();
187
    }
188
    err_end;
189
    printf("f8 - exiting\n");
190
}
191
192
void f9()
193
{
194
    printf("f9 - try, throw, catch, try, throw, catch - entering\n");
195
    err_try
196
    {
197
        tryItOut();
198
    }
199
    err_catch(exc)
200
    {
201
        handleIt(exc);
202
        printf("(trying nested handler within catch) ");
203
        err_try
204
        {
205
            tryItOut();
206
        }
207
        err_catch(exc2)
208
        {
209
            printf("(this is the nested catch) ");
210
            handleIt(exc2);
211
        }
212
        err_end;
213
    }
214
    err_end;
215
    printf("f9 - exiting\n");
216
}
217
218
void f10()
219
{
220
    printf("f10 - try, throw, catch, then call f2 (try, throw, catch) "
221
           "from within catch\n");
222
    err_try
223
    {
224
        tryItOut();
225
    }
226
    err_catch(exc)
227
    {
228
        printf("(catch handler - calling f2)\n");
229
        f2();
230
    }
231
    err_end;
232
    printf("f10 - exiting\n");
233
}
234
235
void f11()
236
{
237
    printf("f11 - try, throw, catch, rethrow\n");
238
    err_try
239
    {
240
        err_try
241
        {
242
            tryItOut();
243
        }
244
        err_catch(exc2)
245
        {
246
            printf("(inner handler) ");
247
            showIt(exc2);
248
            printf("- rethrowing the same exception\n");
249
            err_rethrow();
250
        }
251
        err_end;
252
    }
253
    err_catch(exc)
254
    {
255
        printf("(outer handler) ");
256
        showIt(exc);
257
    }
258
    err_end;
259
    printf("f11 - exiting\n");
260
}
261
262
int main()
263
{
264
    /* initialize for testing */
265
    test_init();
266
267
    /* initialize the error subsystem */
268
    err_init(1024);
269
    
270
    printf("main - starting\n");
271
    err_try
272
    {
273
        printf("main - calling f1\n");
274
275
        err_try
276
        {
277
            f1();
278
        }
279
        err_catch(exc)
280
        {
281
            printf("main - caught exception from f1 - ");
282
            showIt(exc);
283
        }
284
        err_end;
285
286
        printf("main - back from f1, calling f2\n");
287
        f2();
288
        printf("main - back from f2, calling f3\n");
289
        f3();
290
        printf("main - back from f3, calling f4\n");
291
        f4();
292
        printf("main - back from f4, calling f5\n");
293
        f5();
294
295
        err_try
296
        {
297
            printf("main - back from f5, calling f6\n");
298
            f6();
299
        }
300
        err_catch(exc)
301
        {
302
            printf("main - caught exception from f6 - ");
303
            showIt(exc);
304
        }
305
        err_end;
306
307
        err_try
308
        {
309
            printf("main - back from f6, calling f7\n");
310
            f7();
311
        }
312
        err_catch(exc)
313
        {
314
            printf("main - caught exception from f7 - ");
315
            showIt(exc);
316
        }
317
        err_end;
318
319
        err_try
320
        {
321
            printf("main - back from f7, calling f8\n");
322
            f8();
323
        }
324
        err_catch(exc)
325
        {
326
            printf("main - caught exception from f8 - ");
327
            showIt(exc);
328
        }
329
        err_end;
330
331
        printf("main - back from f8, calling f9\n");
332
        f9();
333
        printf("main - back from f9, calling f10\n");
334
        f10();
335
        printf("main - back from f10, calling f11\n");
336
        f11();
337
        printf("main - back from f11\n");
338
    }
339
    err_catch(exc)
340
    {
341
        printf("main - caught exception - ");
342
        showIt(exc);
343
    }
344
    err_end;
345
346
    printf("main - done; stack depth = %d\n", err_stack_depth());
347
    err_terminate();
348
349
    return 0;
350
}
351