Debian Codegraph @

tests/error.test

raw
1 # Commands covered: error, catch, throw, try
2 #
3 # This file contains a collection of tests for one or more of the Tcl built-in
4 # commands. Sourcing this file into Tcl runs the tests and generates output
5 # for errors. No output means no errors were found.
6 #
7 # Copyright (c) 1991-1993 The Regents of the University of California.
8 # Copyright (c) 1994-1996 Sun Microsystems, Inc.
9 # Copyright (c) 1998-1999 by Scriptics Corporation.
10 #
11 # See the file "license.terms" for information on usage and redistribution of
12 # this file, and for a DISCLAIMER OF ALL WARRANTIES.
13
14 if {"::tcltest" ni [namespace children]} {
15 package require tcltest 2.5
16 namespace import -force ::tcltest::*
17 }
18
19 testConstraint memory [llength [info commands memory]]
20 customMatch pairwise {apply {{a b} {
21 string equal [lindex $b 0] [lindex $b 1]
22 }}}
23 namespace eval ::tcl::test::error {
24 if {[testConstraint memory]} {
25 proc getbytes {} {
26 set lines [split [memory info] \n]
27 return [lindex $lines 3 3]
28 }
29 proc leaktest {script {iterations 3}} {
30 set end [getbytes]
31 for {set i 0} {$i < $iterations} {incr i} {
32 uplevel 1 $script
33 set tmp $end
34 set end [getbytes]
35 }
36 return [expr {$end - $tmp}]
37 }
38 }
39
40 proc foo {} {
41 global errorInfo
42 set a [catch {format [error glorp2]} b]
43 error {Human-generated}
44 }
45
46 proc foo2 {} {
47 global errorInfo
48 set a [catch {format [error glorp2]} b]
49 error {Human-generated} $errorInfo
50 }
51
52 # Catch errors occurring in commands and errors from "error" command
53
54 test error-1.1 {simple errors from commands} {
55 catch {format [string index]} b
56 } 1
57 test error-1.2 {simple errors from commands} {
58 catch {format [string index]} b
59 set b
60 } {wrong # args: should be "string index string charIndex"}
61 test error-1.3 {simple errors from commands} {
62 catch {format [string index]} b
63 set ::errorInfo
64 # This used to return '... while executing ...', but string index is fully
65 # compiled as of 8.4a3
66 } {wrong # args: should be "string index string charIndex"
67 while executing
68 "string index"}
69 test error-1.4 {simple errors from commands} {
70 catch {error glorp} b
71 } 1
72 test error-1.5 {simple errors from commands} {
73 catch {error glorp} b
74 set b
75 } glorp
76 test error-1.6 {simple errors from commands} {
77 catch {catch a b c d} b
78 } 1
79 test error-1.7 {simple errors from commands} {
80 catch {catch a b c d} b
81 set b
82 } {wrong # args: should be "catch script ?resultVarName? ?optionVarName?"}
83 test error-1.8 {simple errors from commands} {
84 # This test is non-portable: it generates a memory fault on machines like
85 # DEC Alphas (infinite recursion overflows stack?)
86 #
87 # That claims sounds like a bug to be fixed rather than a portability
88 # problem. Anyhow, I believe it's out of date (bug's been fixed) so this
89 # test is re-enabled.
90 proc p {} {
91 uplevel 1 catch p error
92 }
93 p
94 } 0
95
96 # Check errors nested in procedures. Also check the optional argument to
97 # "error" to generate a new error trace.
98
99 test error-2.1 {errors in nested procedures} {
100 catch foo b
101 } 1
102 test error-2.2 {errors in nested procedures} {
103 catch foo b
104 set b
105 } {Human-generated}
106 test error-2.3 {errors in nested procedures} {
107 catch foo b
108 set ::errorInfo
109 } {Human-generated
110 while executing
111 "error {Human-generated}"
112 (procedure "foo" line 4)
113 invoked from within
114 "foo"}
115 test error-2.4 {errors in nested procedures} {
116 catch foo2 b
117 } 1
118 test error-2.5 {errors in nested procedures} {
119 catch foo2 b
120 set b
121 } {Human-generated}
122 test error-2.6 {errors in nested procedures} {
123 catch foo2 b
124 set ::errorInfo
125 } {glorp2
126 while executing
127 "error glorp2"
128 (procedure "foo2" line 3)
129 invoked from within
130 "foo2"}
131
132 # Error conditions related to "catch".
133
134 test error-3.1 {errors in catch command} {
135 list [catch {catch} msg] $msg
136 } {1 {wrong # args: should be "catch script ?resultVarName? ?optionVarName?"}}
137 test error-3.2 {errors in catch command} {
138 list [catch {catch a b c} msg] $msg
139 } {0 1}
140 test error-3.3 {errors in catch command} {
141 catch {unset a}
142 set a(0) 22
143 list [catch {catch {format 44} a} msg] $msg
144 } {1 {can't set "a": variable is array}}
145 catch {unset a}
146
147 # More tests related to errorInfo and errorCode
148
149 test error-4.1 {errorInfo and errorCode variables} {
150 list [catch {error msg1 msg2 msg3} msg] $msg $::errorInfo $::errorCode
151 } {1 msg1 msg2 msg3}
152 test error-4.2 {errorInfo and errorCode variables} {
153 list [catch {error msg1 {} msg3} msg] $msg $::errorInfo $::errorCode
154 } {1 msg1 {msg1
155 while executing
156 "error msg1 {} msg3"} msg3}
157 test error-4.3 {errorInfo and errorCode variables} {
158 list [catch {error msg1 {}} msg] $msg $::errorInfo $::errorCode
159 } {1 msg1 {msg1
160 while executing
161 "error msg1 {}"} NONE}
162 test error-4.4 {errorInfo and errorCode variables} {
163 set ::errorCode bogus
164 list [catch {error msg1} msg] $msg $::errorInfo $::errorCode
165 } {1 msg1 {msg1
166 while executing
167 "error msg1"} NONE}
168 test error-4.5 {errorInfo and errorCode variables} {
169 set ::errorCode bogus
170 list [catch {error msg1 msg2 {}} msg] $msg $::errorInfo $::errorCode
171 } {1 msg1 msg2 {}}
172
173 test error-4.6 {errorstack via info } -body {
174 proc f x {g $x$x}
175 proc g x {error G:$x}
176 catch {f 12}
177 info errorstack
178 } -match glob -result {INNER * CALL {g 1212} CALL {f 12} UP 1}
179 test error-4.7 {errorstack via options dict } -body {
180 proc f x {g $x$x}
181 proc g x {error G:$x}
182 catch {f 12} m d
183 dict get $d -errorstack
184 } -match glob -result {INNER * CALL {g 1212} CALL {f 12} UP 1}
185 test error-4.8 {errorstack from exec traces} -body {
186 proc foo args {}
187 proc goo {} foo
188 trace add execution foo enter {error bar;#}
189 catch goo m d
190 dict get $d -errorstack
191 } -cleanup {
192 rename goo {}; rename foo {}
193 unset -nocomplain m d
194 } -result {INNER {error bar} CALL goo UP 1}
195
196 # Errors in error command itself
197
198 test error-5.1 {errors in error command} {
199 list [catch {error} msg] $msg
200 } {1 {wrong # args: should be "error message ?errorInfo? ?errorCode?"}}
201 test error-5.2 {errors in error command} {
202 list [catch {error a b c d} msg] $msg
203 } {1 {wrong # args: should be "error message ?errorInfo? ?errorCode?"}}
204
205 # Make sure that catch resets error information
206
207 test error-6.1 {catch must reset error state} {
208 catch {error outer [catch {error inner inner.errorInfo inner.errorCode}]}
209 list $::errorCode $::errorInfo
210 } {NONE 1}
211 test error-6.2 {catch must reset error state} {
212 catch {error outer [catch {return -level 0 -code error -errorcode BUG}]}
213 list $::errorCode $::errorInfo
214 } {NONE 1}
215 test error-6.3 {catch must reset error state} {
216 set ::errorCode BUG
217 catch {error outer [catch set]}
218 list $::errorCode $::errorInfo
219 } {NONE 1}
220 test error-6.4 {catch must reset error state} {
221 catch {error [catch {error foo bar baz}] 1}
222 list $::errorCode $::errorInfo
223 } {NONE 1}
224 test error-6.5 {catch must reset error state} {
225 catch {error [catch {return -level 0 -code error -errorcode BUG}] 1}
226 list $::errorCode $::errorInfo
227 } {NONE 1}
228 test error-6.6 {catch must reset error state} {
229 catch {return -level 0 -code error -errorinfo [catch {error foo bar baz}]}
230 list $::errorCode $::errorInfo
231 } {NONE 1}
232 test error-6.7 {catch must reset error state} {
233 proc foo {} {
234 return -code error -errorinfo [catch {error foo bar baz}]
235 }
236 catch foo
237 list $::errorCode
238 } {NONE}
239 test error-6.8 {catch must reset error state} {
240 catch {return -level 0 -code error [catch {error foo bar baz}]}
241 list $::errorCode
242 } {NONE}
243 test error-6.9 {catch must reset error state} {
244 proc foo {} {
245 return -code error [catch {error foo bar baz}]
246 }
247 catch foo
248 list $::errorCode
249 } {NONE}
250 test error-6.10 {catch must reset errorstack} -body {
251 proc f x {g $x$x}
252 proc g x {error G:$x}
253 catch {f 12}
254 set e1 [info errorstack]
255 catch {f 13}
256 set e2 [info errorstack]
257 list $e1 $e2
258 } -match glob -result {{INNER * CALL {g 1212} CALL {f 12} UP 1} {INNER * CALL {g 1313} CALL {f 13} UP 1}}
259
260 test error-7.1 {Bug 1397843} -body {
261 variable cmds
262 proc EIWrite args {
263 variable cmds
264 lappend cmds [lindex [info level -2] 0]
265 }
266 proc BadProc {} {
267 set i a
268 incr i
269 }
270 trace add variable ::errorInfo write [namespace code EIWrite]
271 catch BadProc
272 trace remove variable ::errorInfo write [namespace code EIWrite]
273 set cmds
274 } -match glob -result {*BadProc*}
275
276 # throw tests
277
278 test error-8.1 {throw produces error 1 at level 0} {
279 catch { throw FOO bar }
280 } {1}
281 test error-8.2 {throw behaves as error does at level 0} {
282 catch { throw FOO bar } em1 opts1
283 catch { error bar {} FOO } em2 opts2
284 dict set opts1 -result $em1
285 dict set opts2 -result $em2
286 foreach key {-code -level -result -errorcode} {
287 if { [dict get $opts1 $key] ne [dict get $opts2 $key] } {
288 error "error/throw outcome differs on '$key'"
289 }
290 }
291 } {}
292 test error-8.3 {throw produces error 1 at level > 0} {
293 proc throw_foo {} {
294 throw FOO bar
295 }
296 catch { throw_foo }
297 } {1}
298 test error-8.4 {throw behaves as error does at level > 0} {
299 proc throw_foo {} {
300 throw FOO bar
301 }
302 proc error_foo {} {
303 error bar {} FOO
304 }
305 catch { throw_foo } em1 opts1
306 catch { error_foo } em2 opts2
307 dict set opts1 -result $em1
308 dict set opts2 -result $em2
309 foreach key {-code -level -result -errorcode} {
310 if { [dict get $opts1 $key] ne [dict get $opts2 $key] } {
311 error "error/throw outcome differs on '$key'"
312 }
313 }
314 } {}
315 test error-8.5 {throw syntax checks} -returnCodes error -body {
316 throw
317 } -result {wrong # args: should be "throw type message"}
318 test error-8.6 {throw syntax checks} -returnCodes error -body {
319 throw a
320 } -result {wrong # args: should be "throw type message"}
321 test error-8.7 {throw syntax checks} -returnCodes error -body {
322 throw a b c
323 } -result {wrong # args: should be "throw type message"}
324 test error-8.8 {throw syntax checks} -returnCodes error -body {
325 throw "not a \{ list" foo
326 } -result {unmatched open brace in list}
327 test error-8.9 {throw syntax checks} -returnCodes error -body {
328 throw {} foo
329 } -result {type must be non-empty list}
330 test error-8.10 {Bug 33b7abb8a2: throw stack usage} -returnCodes error -body {
331 apply {code {throw $code foo}} {}
332 } -result {type must be non-empty list}
333 test error-8.11 {Bug 7174354ecb: throw error message} -returnCodes error -body {
334 throw {not {}a list} x[]y
335 } -result {list element in braces followed by "a" instead of space}
336
337 # simple try tests: body completes with code ok
338
339 test error-9.1 {try (ok, empty result) with no handlers} {
340 try list
341 } {}
342 test error-9.2 {try (ok, non-empty result) with no handlers} {
343 try { list a b c }
344 } {a b c}
345 test error-9.3 {try (ok, non-empty result) with trap handler} {
346 try { list a b c } trap {} {} { list d e f }
347 } {a b c}
348 test error-9.4 {try (ok, non-empty result) with on handler} {
349 try { list a b c } on break {} { list d e f }
350 } {a b c}
351 test error-9.5 {try (ok, non-empty result) with on ok handler} {
352 try { list a b c } on ok {} { list d e f }
353 } {d e f}
354 test error-9.6 {try (compilation of simple finaly token only, bug [27520c9b17])} -body {
355 set b {}; set l {}
356 try {lappend l error} finally [lappend l set b]
357 list $l $b
358 } -cleanup {
359 unset -nocomplain b l
360 } -result {{set b error} {}}
361
362 # simple try tests - "on" handler matching
363
364 test error-10.1 {try with on ok} {
365 try { list a b c } on ok {} { list d e f }
366 } {d e f}
367 test error-10.2 {try with on 0} {
368 try { list a b c } on 0 {} { list d e f }
369 } {d e f}
370 test error-10.3 {try with on error (using error)} {
371 try { error a b c } on error {} { list d e f }
372 } {d e f}
373 test error-10.4 {try with on error (using return -code)} {
374 try { return -level 0 -code 1 a } on error {} { list d e f }
375 } {d e f}
376 test error-10.5 {try with on error (using throw)} {
377 try { throw c a } on error {} { list d e f }
378 } {d e f}
379 test error-10.6 {try with on 1 (using error)} {
380 try { error a b c } on 1 {} { list d e f }
381 } {d e f}
382 test error-10.7 {try with on return} {
383 try { return [list a b c] } on return {} { list d e f }
384 } {d e f}
385 test error-10.8 {try with on break} {
386 try { break } on break {} { list d e f }
387 } {d e f}
388 test error-10.9 {try with on continue} {
389 try { continue } on continue {} { list d e f }
390 } {d e f}
391 test error-10.10 {try with on for arbitrary (decimal) return code} {
392 try { return -level 0 -code 123456 } on 123456 {} { list d e f }
393 } {d e f}
394 test error-10.11 {try with on for arbitrary (hex) return code} {
395 try { return -level 0 -code 0x123456 } on 0x123456 {} { list d e f }
396 } {d e f}
397 test error-10.12 {try with on for arbitrary return code (mixed number representations)} {
398 try { return -level 0 -code 0x10 } on 16 {} { list d e f }
399 } {d e f}
400
401 # simple try tests - "trap" handler matching
402
403 test error-11.1 {try with trap all} {
404 try { throw FOO bar } trap {} {} { list d e f }
405 } {d e f}
406 test error-11.2 {try with trap (exact)} {
407 try { throw FOO bar } trap {FOO} {} { list d e f }
408 } {d e f}
409 test error-11.3 {try with trap (prefix 1)} {
410 try { throw [list FOO A B C D] bar } trap {FOO} {} { list d e f }
411 } {d e f}
412 test error-11.4 {try with trap (prefix 2)} {
413 try { throw [list FOO A B C D] bar } trap {FOO A} {} { list d e f }
414 } {d e f}
415 test error-11.5 {try with trap (prefix 3)} {
416 try { throw [list FOO A B C D] bar } trap {FOO A B} {} { list d e f }
417 } {d e f}
418 test error-11.6 {try with trap (prefix 4)} {
419 try { throw [list FOO A B C D] bar } trap {FOO A B C} {} { list d e f }
420 } {d e f}
421 test error-11.7 {try with trap (exact, 5 elements)} {
422 try { throw [list FOO A B C D] bar } trap {FOO A B C D} {} { list d e f }
423 } {d e f}
424
425 # simple try tests - variable assignment and result handling
426
427 test error-12.1 {try with no variable assignment in on handler} {
428 try { throw FOO bar } on error {} { list d e f }
429 } {d e f}
430 test error-12.2 {try with result variable assignment in on handler} {
431 try { throw FOO bar } on error {res} { set res }
432 } {bar}
433 test error-12.3 {try with result variable assignment in on handler, var remains in scope} {
434 try { throw FOO bar } on error {res} { list d e f }
435 set res
436 } {bar}
437 test error-12.4 {try with result/opts variable assignment in on handler} {
438 try {
439 throw FOO bar
440 } on error {res opts} {
441 set r "$res,[dict get $opts -errorcode]"
442 }
443 } {bar,FOO}
444 test error-12.5 {try with result/opts variable assignment in on handler, vars remain in scope} {
445 try { throw FOO bar } on error {res opts} { list d e f }
446 set r "$res,[dict get $opts -errorcode]"
447 } {bar,FOO}
448 test error-12.6 {try result is propagated if no matching handler} {
449 try { list a b c } on error {} { list d e f }
450 } {a b c}
451 test error-12.7 {handler result is propagated if handler executes} {
452 try { throw FOO bar } on error {} { list d e f }
453 } {d e f}
454
455 # negative case try tests - bad args to try
456
457 test error-13.1 {try with no arguments} -body {
458 # warning: error message may change
459 try
460 } -returnCodes error -match glob -result {wrong # args: *}
461 test error-13.2 {try with body only (ok)} {
462 try list
463 } {}
464 test error-13.3 {try with missing finally body} -body {
465 # warning: error message may change
466 try list finally
467 } -returnCodes error -match glob -result {wrong # args to finally clause: *}
468 test error-13.4 {try with bad handler keyword} -body {
469 # warning: error message may change
470 try list then a b c
471 } -returnCodes error -match glob -result {bad handler *}
472 test error-13.5 {try with partial handler #1} -body {
473 # warning: error message may change
474 try list on
475 } -returnCodes error -match glob -result {wrong # args to on clause: *}
476 test error-13.6 {try with partial handler #2} -body {
477 # warning: error message may change
478 try list on error
479 } -returnCodes error -match glob -result {wrong # args to on clause: *}
480 test error-13.7 {try with partial handler #3} -body {
481 # warning: error message may change
482 try list on error {em opts}
483 } -returnCodes error -match glob -result {wrong # args to on clause: *}
484 test error-13.8 {try with multiple handlers and finally (ok)} {
485 try list on error {} {} trap {} {} {} finally {}
486 } {}
487 test error-13.9 {last handler body can't be a fallthrough #1} -body {
488 try list on error {} {} on break {} -
489 } -returnCodes error -result {last non-finally clause must not have a body of "-"}
490 test error-13.10 {last handler body can't be a fallthrough #2} -body {
491 try list on error {} {} on break {} - finally { list d e f }
492 } -returnCodes error -result {last non-finally clause must not have a body of "-"}
493
494 # try tests - multiple handlers (left-to-right matching, only one runs)
495
496 test error-14.1 {try with multiple handlers (only one matches) #1} {
497 try { throw FOO bar } on ok {} { list a b c } trap FOO {} { list d e f }
498 } {d e f}
499 test error-14.2 {try with multiple handlers (only one matches) #2} {
500 try { throw FOO bar } trap FOO {} { list d e f } on ok {} { list a b c }
501 } {d e f}
502 test error-14.3 {try with multiple handlers (only one matches) #3} {
503 try {
504 throw FOO bar
505 } on break {} {
506 list x y z
507 } trap FOO {} {
508 list d e f
509 } on ok {} {
510 list a b c
511 }
512 } {d e f}
513 test error-14.4 {try with multiple matching handlers (only the first in left-to-right order runs) #1} {
514 try { throw FOO bar } on error {} { list a b c } trap FOO {} { list d e f }
515 } {a b c}
516 test error-14.5 {try with multiple matching handlers (only the first in left-to-right order runs) #2} {
517 try { throw FOO bar } trap FOO {} { list d e f } on error {} { list a b c }
518 } {d e f}
519 test error-14.6 {try with multiple matching handlers (only the first in left-to-right order runs) #3} {
520 try { throw FOO bar } trap {} {} { list d e f } on 1 {} { list a b c }
521 } {d e f}
522 test error-14.7 {try with multiple matching handlers (only the first in left-to-right order runs) #4} {
523 try { throw FOO bar } on 1 {} { list a b c } trap {} {} { list d e f }
524 } {a b c}
525 test error-14.8 {try with handler-of-last-resort "trap {}"} {
526 try { throw FOO bar } trap FOX {} { list a b c } trap {} {} { list d e f }
527 } {d e f}
528 test error-14.9 {try with handler-of-last-resort "on error"} {
529 try { foo } trap FOX {} { list a b c } on error {} { list d e f }
530 } {d e f}
531
532 # try tests - propagation (no matching handlers)
533
534 test error-15.1 {try with no handler (ok result propagates)} {
535 try { list a b c }
536 } {a b c}
537 test error-15.2 {try with no matching handler (ok result propagates)} {
538 try { list a b c } on error {} { list d e f }
539 } {a b c}
540 test error-15.3 {try with no handler (error result propagates)} -body {
541 try { throw FOO bar }
542 } -returnCodes error -result {bar}
543 test error-15.4 {try with no matching handler (error result propagates)} -body {
544 try { throw FOO bar } trap FOX {} { list a b c }
545 } -returnCodes error -result {bar}
546 test error-15.5 {try with no handler (return result propagates)} -body {
547 try { return bar }
548 } -returnCodes 2 -result {bar}
549 test error-15.6 {try with no matching handler (break result propagates)} -body {
550 try { if {1} break } on error {} { list a b c }
551 } -returnCodes 3 -result {}
552 test error-15.7 {try with no matching handler (unknown integer result propagates)} -body {
553 try { return -level 0 -code 123456 } trap {} {} { list a b c }
554 } -returnCodes 123456 -result {}
555
556 foreach level {0 1 2 3} {
557 foreach code {0 1 2 3 4 5} {
558
559 # Following cases have different -errorinfo; avoid false alarms
560 # TODO: examine whether these difference are as they ought to be.
561 if {$level == 0 && $code == 1} continue
562
563 foreach extras {{} {-bar soom}} {
564
565 test error-15.8.$level.$code.[llength $extras] {[try] coverage} {
566 set script {return -level $level -code $code {*}$extras foo}
567 catch $script m1 o1
568 catch {try $script} m2 o2
569 set o1 [lsort -stride 2 $o1]
570 set o2 [lsort -stride 2 $o2]
571 expr {$o1 eq $o2 ? "ok" : "$o1\n\tis not equal to\n$o2"}
572 } ok
573
574 test error-15.9.$level.$code.[llength $extras] {[try] coverage} {
575 set script {return -level $level -code $code {*}$extras foo}
576 catch $script m1 o1
577 catch {try $script finally {}} m2 o2
578 set o1 [lsort -stride 2 $o1]
579 set o2 [lsort -stride 2 $o2]
580 expr {$o1 eq $o2 ? "ok" : "$o1\n\tis not equal to\n$o2"}
581 } ok
582
583 test error-15.10.$level.$code.[llength $extras] {[try] coverage} {
584 set script {return -level $level -code $code {*}$extras foo}
585 catch $script m1 o1
586 catch {try $script on $code {x y} {return -options $y $x}} m2 o2
587 set o1 [lsort -stride 2 $o1]
588 set o2 [lsort -stride 2 $o2]
589 expr {$o1 eq $o2 ? "ok" : "$o1\n\tis not equal to\n$o2"}
590 } ok
591
592 }
593 }
594 }
595
596 # try tests - propagation (exceptions in handlers, exception chaining)
597
598 test error-16.1 {try with successfully executed handler} {
599 try { throw FOO bar } trap FOO {} { list a b c }
600 } {a b c}
601 test error-16.2 {try with exception (error) in handler} -body {
602 try { throw FOO bar } trap FOO {} { throw BAR foo }
603 } -returnCodes error -result {foo}
604 test error-16.3 {try with exception (return) in handler} -body {
605 try { throw FOO bar } trap FOO {} { return BAR }
606 } -returnCodes 2 -result {BAR}
607 test error-16.4 {try with exception (break) in handler #1} -body {
608 try { throw FOO bar } trap FOO {} { break }
609 } -returnCodes 3 -result {}
610 test error-16.5 {try with exception (break) in handler #2} {
611 for { set i 5 } { $i < 10 } { incr i } {
612 try { throw FOO bar } trap FOO {} { break }
613 }
614 set i
615 } {5}
616 test error-16.6 {try with variable assignment and propagation #1} {
617 # Ensure that the handler variables preserve the exception off the
618 # try-body, and are not modified by the exception off the handler
619 catch {
620 try { throw FOO bar } trap FOO {em} { throw BAR baz }
621 }
622 set em
623 } {bar}
624 test error-16.7 {try with variable assignment and propagation #2} {
625 catch {
626 try { throw FOO bar } trap FOO {em opts} { throw BAR baz }
627 }
628 list $em [dict get $opts -errorcode]
629 } {bar FOO}
630 test error-16.8 {exception chaining (try=ok, handler=error)} -body {
631 #FIXME is the intent of this test correct?
632 catch {
633 try { list a b c } on ok {em opts} { throw BAR baz }
634 } tryem tryopts
635 list $opts [dict get $tryopts -during]
636 } -match pairwise -result equal
637 test error-16.9 {exception chaining (try=error, handler=error)} -body {
638 # The exception off the handler should chain to the exception off the
639 # try-body (using the -during option)
640 catch {
641 try { throw FOO bar } trap {} {em opts} { throw BAR baz }
642 } tryem tryopts
643 list $opts [dict get $tryopts -during]
644 } -match pairwise -result equal
645 test error-16.10 {no exception chaining when handler is successful} {
646 catch {
647 try { throw FOO bar } trap {} {em opts} { list d e f }
648 } tryem tryopts
649 dict exists $tryopts -during
650 } {0}
651 test error-16.11 {no exception chaining when handler is a non-error exception} {
652 catch {
653 try { throw FOO bar } trap {} {em opts} { break }
654 } tryem tryopts
655 dict exists $tryopts -during
656 } {0}
657 test error-16.12 {compiled try with successfully executed handler} {
658 apply {{} {
659 try { throw FOO bar } trap FOO {} { list a b c }
660 }}
661 } {a b c}
662 test error-16.13 {compiled try with exception (error) in handler} -body {
663 apply {{} {
664 try { throw FOO bar } trap FOO {} { throw BAR foo }
665 }}
666 } -returnCodes error -result {foo}
667 test error-16.14 {compiled try with exception (return) in handler} -body {
668 apply {{} {
669 list [catch {
670 try { throw FOO bar } trap FOO {} { return BAR }
671 } msg] $msg
672 }}
673 } -result {2 BAR}
674 test error-16.15 {compiled try with exception (break) in handler} {
675 apply {{} {
676 for { set i 5 } { $i < 10 } { incr i } {
677 try { throw FOO bar } trap FOO {} { break }
678 }
679 return $i
680 }}
681 } {5}
682 test error-16.16 {compiled try with exception (continue) in handler} {
683 apply {{} {
684 for { set i 5 } { $i < 10 } { incr i } {
685 try { throw FOO bar } trap FOO {} { continue }
686 incr i 20
687 }
688 return $i
689 }}
690 } {10}
691 test error-16.17 {compiled try with variable assignment and propagation #1} {
692 # Ensure that the handler variables preserve the exception off the
693 # try-body, and are not modified by the exception off the handler
694 apply {{} {
695 catch {
696 try { throw FOO bar } trap FOO {em} { throw BAR baz }
697 }
698 return $em
699 }}
700 } {bar}
701 test error-16.18 {compiled try with variable assignment and propagation #2} {
702 apply {{} {
703 catch {
704 try { throw FOO bar } trap FOO {em opts} { throw BAR baz }
705 }
706 list $em [dict get $opts -errorcode]
707 }}
708 } {bar FOO}
709 test error-16.19 {compiled try exception chaining (try=ok, handler=error)} -body {
710 #FIXME is the intent of this test correct?
711 apply {{} {
712 catch {
713 try { list a b c } on ok {em opts} { throw BAR baz }
714 } tryem tryopts
715 list $opts [dict get $tryopts -during]
716 }}
717 } -match pairwise -result equal
718 test error-16.20 {compiled try exception chaining (try=error, handler=error)} -body {
719 # The exception off the handler should chain to the exception off the
720 # try-body (using the -during option)
721 apply {{} {
722 catch {
723 try { throw FOO bar } trap {} {em opts} { throw BAR baz }
724 } tryem tryopts
725 list $opts [dict get $tryopts -during]
726 }}
727 } -match pairwise -result equal
728 test error-16.21 {compiled try exception chaining (try=error, finally=error)} {
729 # The exception off the handler should chain to the exception off the
730 # try-body (using the -during option)
731 apply {{} {
732 catch {
733 try { throw FOO bar } finally { throw BAR baz }
734 } tryem tryopts
735 dict get $tryopts -during -errorcode
736 }}
737 } FOO
738 test error-16.22 {compiled try: no exception chaining when handler is successful} {
739 apply {{} {
740 catch {
741 try { throw FOO bar } trap {} {em opts} { list d e f }
742 } tryem tryopts
743 dict exists $tryopts -during
744 }}
745 } {0}
746 test error-16.23 {compiled try: no exception chaining when handler is a non-error exception} {
747 apply {{} {
748 catch {
749 try { throw FOO bar } trap {} {em opts} { break }
750 } tryem tryopts
751 dict exists $tryopts -during
752 }}
753 } {0}
754 test error-16.24 {compiled try exception chaining (try=ok, handler=error, finally=error)} -body {
755 apply {{} {
756 catch {
757 try {
758 list a b c
759 } on ok {em opts} {
760 throw BAR baz
761 } finally {
762 throw DING dong
763 }
764 } tryem tryopts
765 list $opts [dict get $tryopts -during -during]
766 }}
767 } -match pairwise -result equal
768 test error-16.25 {compiled try exception chaining (all errors)} -body {
769 apply {{} {
770 catch {
771 try {
772 throw FOO bar
773 } on error {em opts} {
774 throw BAR baz
775 } finally {
776 throw DING dong
777 }
778 } tryem tryopts
779 list $opts [dict get $tryopts -during -during]
780 }}
781 } -match pairwise -result equal
782
783 # try tests - finally
784
785 test error-17.1 {finally always runs (try with ok result)} {
786 set RES {}
787 try { list a b c } finally { set RES done }
788 set RES
789 } {done}
790 test error-17.2 {finally always runs (try with error result)} {
791 set RES {}
792 catch {
793 try { throw FOO bar } finally { set RES done }
794 }
795 set RES
796 } {done}
797 test error-17.3 {finally always runs (try with matching handler)} {
798 set RES {}
799 try { throw FOO bar } trap FOO {} { list a b c } finally { set RES done }
800 set RES
801 } {done}
802 test error-17.4 {finally always runs (try with exception in handler)} {
803 set RES {}
804 catch {
805 try {
806 throw FOO bar
807 } trap FOO {} {
808 throw BAR baz
809 } finally {
810 set RES done
811 }
812 }
813 set RES
814 } {done}
815 test error-17.5 {successful finally doesn't modify try outcome (try=ok)} {
816 try { list a b c } finally { list d e f }
817 } {a b c}
818 test error-17.6 {successful finally doesn't modify try outcome (try=return)} -body {
819 try { return c } finally { list d e f }
820 } -returnCodes 2 -result {c}
821 test error-17.7 {successful finally doesn't modify try outcome (try=error)} -body {
822 try { error bar } finally { list d e f }
823 } -returnCodes 1 -result {bar}
824 test error-17.8 {successful finally doesn't modify handler outcome (handler=ok)} {
825 try { throw FOO bar } trap FOO {} { list a b c } finally { list d e f }
826 } {a b c}
827 test error-17.9 {successful finally doesn't modify handler outcome (handler=error)} -body {
828 try { throw FOO bar } trap FOO {} { throw BAR baz } finally { list d e f }
829 } -returnCodes error -result {baz}
830 test error-17.10 {successful finally doesn't affect variable assignment} {
831 catch {
832 try { throw FOO bar } trap FOO {em opts} { list d e f } finally { list d e f }
833 } result
834 list $em $result
835 } {bar {d e f}}
836 test error-17.11 {successful finally doesn't affect variable assignment or propagation} {
837 catch {
838 try { throw FOO bar } trap FOO {em opts} { throw BAR baz } finally { list d e f }
839 }
840 list $em [dict get $opts -errorcode]
841 } {bar FOO}
842
843 # try tests - propagation (exceptions in finally, exception chaining)
844
845 test error-18.1 {try (ok) with exception in finally (error)} -body {
846 try { list a b c } finally { throw BAR foo }
847 } -returnCodes error -result {foo}
848 test error-18.2 {try (error) with exception in finally (break)} -body {
849 try { throw FOO bar } finally { break }
850 } -returnCodes 3 -result {}
851 test error-18.3 {try (ok) with handler (ok) and exception in finally (error)} -body {
852 try { list a b c } on ok {} { list d e f } finally { throw BAR foo }
853 } -returnCodes error -result {foo}
854 test error-18.4 {try (error) with exception in handler (error) and in finally (arb code)} -body {
855 try { throw FOO bar } on error {} { throw BAR baz } finally { return -level 0 -code 99 zing }
856 } -returnCodes 99 -result {zing}
857 test error-18.5 {exception in finally doesn't affect variable assignment} {
858 catch {
859 try { throw FOO bar } trap FOO {em opts} { throw BAR baz } finally { throw BAZ zing }
860 }
861 list $em [dict get $opts -errorcode]
862 } {bar FOO}
863 test error-18.6 {exception chaining in finally (try=ok)} -body {
864 catch {
865 list a b c
866 } em expopts
867 catch {
868 try { list a b c } finally { throw BAR foo }
869 } em opts
870 list $expopts [dict get $opts -during]
871 } -match pairwise -result equal
872 test error-18.7 {exception chaining in finally (try=error)} {
873 catch {
874 try { throw FOO bar } finally { throw BAR baz }
875 } em opts
876 dict get $opts -during -errorcode
877 } {FOO}
878 test error-18.8 {exception chaining in finally (try=ok, handler=ok)} {
879 catch {
880 try { list a b c } on ok {} { list d e f } finally { throw BAR baz }
881 } em opts
882 list [dict get $opts -during -code] [dict exists $opts -during -during]
883 } {0 0}
884 test error-18.9 {exception chaining in finally (try=error, handler=ok)} {
885 catch {
886 try {
887 throw FOO bar
888 } on error {} {
889 list d e f
890 } finally {
891 throw BAR baz
892 }
893 } em opts
894 list [dict get $opts -during -code] [dict exists $opts -during -during]
895 } {0 0}
896 test error-18.10 {exception chaining in finally (try=error, handler=error)} {
897 catch {
898 try {
899 throw FOO bar
900 } on error {} {
901 throw BAR baz
902 } finally {
903 throw BAR baz
904 }
905 } em opts
906 list [dict get $opts -during -errorcode] [dict get $opts -during -during -errorcode]
907 } {BAR FOO}
908 test error-18.11 {no exception chaining if finally produces a non-error exception} {
909 catch {
910 try { throw FOO bar } on error {} { throw BAR baz } finally { break }
911 } em opts
912 dict exists $opts -during
913 } {0}
914 test error-18.12 {variable assignment unaffected by exception in finally} {
915 catch {
916 try {
917 throw FOO bar
918 } on error {em opts} {
919 list a b c
920 } finally {
921 throw BAR baz
922 }
923 }
924 list $em [dict get $opts -errorcode]
925 } {bar FOO}
926
927 # try tests - fall-through body cases
928
929 test error-19.1 {try with fallthrough body #1} {
930 set RES {}
931 try { list a b c } on ok { set RES 0 } - on error {} { set RES 1 }
932 set RES
933 } {1}
934 test error-19.2 {try with fallthrough body #2} {
935 set RES {}
936 try {
937 throw FOO bar
938 } trap BAR {} {
939 } trap FOO {} - trap {} {} {
940 set RES foo
941 } on error {} {
942 set RES err
943 }
944 set RES
945 } {foo}
946 test error-19.3 {try with cascade fallthrough} {
947 set RES {}
948 try {
949 throw FOO bar
950 } trap FOO {} - trap BAR {} - trap {} {} {
951 set RES trap
952 } on error {} { set RES err }
953 set RES
954 } {trap}
955 test error-19.4 {multiple unrelated fallthroughs #1} {
956 set RES {}
957 try {
958 throw FOO bar
959 } trap FOO {} - trap BAR {} {
960 set RES foo
961 } trap {} {} - on error {} {
962 set RES err
963 }
964 set RES
965 } {foo}
966 test error-19.5 {multiple unrelated fallthroughs #2} {
967 set RES {}
968 try {
969 throw BAZ zing
970 } trap FOO {} - trap BAR {} {
971 set RES foo
972 } trap {} {} - on error {} {
973 set RES err
974 }
975 set RES
976 } {err}
977 proc addmsg msg {
978 variable RES
979 lappend RES $msg
980 }
981 test error-19.6 {compiled try executes all clauses} -setup {
982 set RES {}
983 } -body {
984 apply {{} {
985 try {
986 addmsg a
987 throw bar hello
988 } trap bar {res opt} {
989 addmsg b
990 } finally {
991 addmsg c
992 }
993 addmsg d
994 } ::tcl::test::error}
995 } -cleanup {
996 unset RES
997 } -result {a b c d}
998 test error-19.7 {compiled try executes all clauses} -setup {
999 set RES {}
1000 } -body {
1001 apply {{} {
1002 try {
1003 addmsg a
1004 } on error {res opt} {
1005 addmsg b
1006 } on ok {} {
1007 addmsg c
1008 } finally {
1009 addmsg d
1010 }
1011 addmsg e
1012 } ::tcl::test::error}
1013 } -cleanup {
1014 unset RES
1015 } -result {a c d e}
1016 test error-19.8 {compiled try executes all clauses} -setup {
1017 set RES {}
1018 } -body {
1019 apply {{} {
1020 try {
1021 addmsg a
1022 throw bar hello
1023 } trap bar {res opt} {
1024 addmsg b
1025 }
1026 addmsg c
1027 } ::tcl::test::error}
1028 } -cleanup {
1029 unset RES
1030 } -result {a b c}
1031 test error-19.9 {compiled try executes all clauses} -setup {
1032 set RES {}
1033 } -body {
1034 apply {{} {
1035 try {
1036 addmsg a
1037 } on error {res opt} {
1038 addmsg b
1039 } on ok {} {
1040 addmsg c
1041 }
1042 addmsg d
1043 } ::tcl::test::error}
1044 } -cleanup {
1045 unset RES
1046 } -result {a c d}
1047 test error-19.10 {compiled try with chained clauses} -setup {
1048 set RES {}
1049 } -body {
1050 list [apply {{} {
1051 try {
1052 return good
1053 } on return {res} - on ok {res} {
1054 addmsg ok
1055 addmsg $res
1056 return handler
1057 } finally {
1058 addmsg finally
1059 }
1060 } ::tcl::test::error}] $RES
1061 } -cleanup {
1062 unset RES
1063 } -result {handler {ok good finally}}
1064 test error-19.11 {compiled try and errors on variable write} -setup {
1065 set RES {}
1066 } -body {
1067 apply {{} {
1068 array set foo {bar boo}
1069 set bar unset
1070 catch {
1071 try {
1072 addmsg body
1073 return a
1074 } on return {bar foo} {
1075 addmsg handler
1076 return b
1077 } finally {
1078 addmsg finally,$bar
1079 }
1080 } msg
1081 addmsg $msg
1082 } ::tcl::test::error}
1083 } -cleanup {
1084 unset RES
1085 } -result {body finally,a {can't set "foo": variable is array}}
1086 test error-19.12 {interpreted try and errors on variable write} -setup {
1087 set RES {}
1088 } -body {
1089 apply {try {
1090 array set foo {bar boo}
1091 set bar unset
1092 catch {
1093 $try {
1094 addmsg body
1095 return a
1096 } on return {bar foo} {
1097 addmsg handler
1098 return b
1099 } finally {
1100 addmsg finally,$bar
1101 }
1102 } msg
1103 addmsg $msg
1104 } ::tcl::test::error} try
1105 } -cleanup {
1106 unset RES
1107 } -result {body finally,a {can't set "foo": variable is array}}
1108 test error-19.13 {compiled try and errors on variable write} -setup {
1109 set RES {}
1110 } -body {
1111 apply {{} {
1112 array set foo {bar boo}
1113 set bar unset
1114 catch {
1115 try {
1116 addmsg body
1117 return a
1118 } on return {bar foo} - on error {bar foo} {
1119 addmsg handler
1120 return b
1121 } finally {
1122 addmsg finally,$bar
1123 }
1124 } msg
1125 addmsg $msg
1126 } ::tcl::test::error}
1127 } -cleanup {
1128 unset RES
1129 } -result {body finally,a {can't set "foo": variable is array}}
1130 rename addmsg {}
1131
1132 # FIXME test what vars get set on fallthough ... what is the correct behavior?
1133 # It would seem appropriate to set at least those for the matching handler and
1134 # the executed body; possibly for each handler we fall through as well?
1135
1136 # negative case try tests - bad "on" handler
1137
1138 test error-20.1 {bad code name in on handler} -body {
1139 try { list a b c } on err {} {}
1140 } -returnCodes error -match glob -result {bad completion code "err": must be ok, error, return, break, continue*, or an integer}
1141 test error-20.2 {bad code value in on handler} -body {
1142 try { list a b c } on 34985723094872345 {} {}
1143 } -returnCodes error -match glob -result {bad completion code "34985723094872345": must be ok, error, return, break, continue*, or an integer}
1144
1145 test error-21.1 {memory leaks in try: Bug 2910044} memory {
1146 leaktest {
1147 try {string repeat x 10} on ok {} {}
1148 }
1149 } 0
1150 test error-21.2 {memory leaks in try: Bug 2910044} memory {
1151 leaktest {
1152 try {error [string repeat x 10]} on error {} {}
1153 }
1154 } 0
1155 test error-21.3 {memory leaks in try: Bug 2910044} memory {
1156 leaktest {
1157 try {throw FOO [string repeat x 10]} trap FOO {} {}
1158 }
1159 } 0
1160 test error-21.4 {memory leaks in try: Bug 2910044} memory {
1161 leaktest {
1162 try {string repeat x 10}
1163 }
1164 } 0
1165 test error-21.5 {memory leaks in try: Bug 2910044} memory {
1166 leaktest {
1167 try {string repeat x 10} on ok {} {} finally {string repeat y 10}
1168 }
1169 } 0
1170 test error-21.6 {memory leaks in try: Bug 2910044} memory {
1171 leaktest {
1172 try {
1173 error [string repeat x 10]
1174 } on error {} {} finally {
1175 string repeat y 10
1176 }
1177 }
1178 } 0
1179 test error-21.7 {memory leaks in try: Bug 2910044} memory {
1180 leaktest {
1181 try {
1182 throw FOO [string repeat x 10]
1183 } trap FOO {} {} finally {
1184 string repeat y 10
1185 }
1186 }
1187 } 0
1188 test error-21.8 {memory leaks in try: Bug 2910044} memory {
1189 leaktest {
1190 try {string repeat x 10} finally {string repeat y 10}
1191 }
1192 } 0
1193
1194 test error-21.9 {Bug cee90e4e88} {
1195 # Just don't panic.
1196 apply {{} {try {} on ok {} - on return {} {}}}
1197 } {}
1198
1199
1200 # negative case try tests - bad "trap" handler
1201 # what is the effect if we attempt to trap an errorcode that is not a list?
1202 # nested try
1203 # catch inside try
1204 # no tests for bad varslist?
1205 # -errorcode but code!=1 doesn't trap
1206 # throw negative case tests (no args, too many args, etc)
1207
1208 }
1209 namespace delete ::tcl::test::error
1210
1211 # cleanup
1212 catch {rename p ""}
1213 ::tcltest::cleanupTests
1214 return
1215
1216 # Local Variables:
1217 # mode: tcl
1218 # End: