Skip to content
Tcl Source Code View Ticket
Login

View Ticket

[catch] leave trace mystery

Closed / Duplicate2390819fffffffffffffffffffffffffffffffffCreated 18 years agoUpdated 14 years ago
Descriptionplain2008-12-04 22:07:45
% proc foo1 {} {
catch {return -code error zoom} err
return [list $err zam]
}
% proc foo2 {} {
catch {
return -code error zoom
} err
return [list $err zam]
}

[foo1] and [foo2] are the same except
for whitespace, and as expected, they
produce the same results:

% foo1
zoom zam
% foo2
zoom zam

Next create same routines again, with
new names, bar1 and bar2:

% proc bar1 {} {
catch {return -code error zoom} err
return [list $err zam]
}
% proc bar2 {} {
catch {
return -code error zoom
} err
return [list $err zam]
}

This time, before compiling and running,
add a leave trace on [catch] :

% trace add execution catch leave {puts zing! ;#}

and try again:

% bar1
zing!
zoom zam
% bar2
zing!
zoom


WTF?!?!?
dgpplain2008-12-05 05:09:52
Then try the whole thing in 8.5.5:

<... cut to the chase ...>

% bar1
zing!
zoom zam
% bar2
zing!
2
dgp Ticket [2390819fff]: 1 change [History diff]2013-01-08 02:04:37
dgpplain2013-03-20 03:18:40
The trouble here is that the trailing " ;# " in 
the execution trace handler doesn't do what
the programmer expected.  It doesn't cause all
arguments passed to the handler to be ignored.
Instead it causes the leading substring in it up
to the first newline to be ignored only.  Then it
starts up again with the rest, which is the command
[return -code error zoom].  Now that that's being
eval'd outside the catch, it bubbles up the stack.

At least that's the explanation for the 8.4 results.
Ugly complex version of pilot error.
dgpplain2013-03-20 03:22:13
Corrected demo works fine:
% info patch
8.4.19
% proc report args {puts zing!}
% trace add execution catch leave report
% proc bar2 {} {
catch {
return -code error zoom
} err
return [list $err zam]
}
% bar2
zing!
zoom zam
dgpplain2013-03-20 03:24:59
Corrected code also works as expected
in 8.5 and 8.6.

Remaining mystery is why the pilot error
version fails differently in 8.5+.  Suspect
it's something not right in how [catch] gets
compiled and executed as bytecode.
dgpplain2013-03-21 11:42:12
allow_comments - 1

Re-reported as 3608676 without so
much noise and confusion.
Log in (anonymous is fine) to add a comment.