Search code examples
tcl

Why can't I access errorInfo and errorCode


I have the following code:

$ cat ~/tmp/2.tcl
set zero 0
proc p1 {} {
    if {[catch {expr 1/$zero} err]} {
        puts "errorCode=$errorCode"
        puts "errorInfo=$errorInfo"
    }
}

p1

When I source it, I get error accessing errorCode:

$ tclsh ~/tmp/2.tcl
can't read "errorCode": no such variable
    while executing
"puts "errorCode=$errorCode""
    (procedure "p1" line 3)
    invoked from within
"p1"
    (file "~/tmp/2.tcl" line 9)

I tried changing to $::errorCode, but did not help.

Can you see what is wrong?


Solution

  • The errorInfo and errorCode variables are globals. You should either use the global command to bring them into scope or use their fully-qualified names (i.e., precede with ::).

    It might be easier to pick the information out of the result options dictionary (a new feature in 8.5).