문제

I have the following proc which basically looks upp a couple of values in a dictonary and returns them as a list.

proc GetAllow { PID Ply } {

# read a dictonary from a file
catch {

append PlyAndDirXt $Ply "_xt"
append PlyAndDirYt $Ply "_yt"

set x_allow_tens [ dict get $allowables $PID $PlyAndDirXt ]
set y_allow_tens [ dict get $allowables $PID $PlyAndDirYt ]
set allowables [ list $x_allow_tens $y_allow_tens ]

} res

if { $res == 0 } {
  return $allowables
} 
if { $res != 0 } {
  return 999
}    

}

As I understand "catch" if everything is ok $res should be 0 = TCL_OK. In that case I would like the proc to return the list $allowables. In case the values are not found in the dict due to none matching keys. I would like it to return 999. But I always get 999 back. What am I'm doing wrong here ?

도움이 되었습니까?

해결책

As per the manual:

If script raises an error, catch will return a non-zero integer value corresponding to the exceptional return code returned by evaluation of script. Tcl defines the normal return code from script evaluation to be zero (0), or TCL_OK.

If the varName argument is given, then the variable it names is set to the result of the script evaluation. When the return code from the script is 1 (TCL_ERROR), the value stored in varName is an error message. When the return code from the script is 0 (TCL_OK), the value stored in resultVarName is the value returned from script.

As such, $res will not be equal to 0 unless the result of your script returns 0.

You can set catch to a variable like this:

set err [catch {

    append PlyAndDirXt $Ply "_xt"
    append PlyAndDirYt $Ply "_yt"
    
    set x_allow_tens [ dict get $allowables $PID $PlyAndDirXt ]
    set y_allow_tens [ dict get $allowables $PID $PlyAndDirYt ]
    set allowables [ list $x_allow_tens $y_allow_tens ]

} res]

Then check

if { $err == 0 } {
  return $allowables ;# Or return $res since that's the last evaluated line
} 
if { $err != 0 } {
  return 999
}  
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top