Error handling TCL,在catch命令中获取完整错误消息
运行此脚本时,会收到错误消息:Error handling TCL,在catch命令中获取完整错误消息,error-handling,tcl,Error Handling,Tcl,运行此脚本时,会收到错误消息: #!/usr/bin/tclsh proc test {} { aaa } test 如果在catch中运行test命令,则只会收到第一行错误消息 invalid command name "aaa" while executing "aaa" (procedure "test" line 2) invoked from within "test" (file "./a.tcl" line 7) 这张照片是
#!/usr/bin/tclsh
proc test {} {
aaa
}
test
如果在catch
中运行test命令,则只会收到第一行错误消息
invalid command name "aaa"
while executing
"aaa"
(procedure "test" line 2)
invoked from within
"test"
(file "./a.tcl" line 7)
这张照片是:
无效的命令名“aaa”
是否可以在catch命令中获取完整的错误消息(文件、行、过程)?我的程序有很多文件,通过只获取一行错误消息,很难从哪里找到它。简单的答案是查看包含堆栈跟踪的
errorInfo
的值
更完整的答案是查看和手册页面,并利用catch
语句中的-options-varname
参数来收集提供的更详细信息。return
手册页面提供了一些有关使用此功能的信息。但一个来自互动会话的粗略示例:
#!/usr/bin/tclsh
proc test {} {
aaa
}
catch test msg
puts $msg
detail
变量是一个字典,因此使用dict get$detail-errorinfo
获取该特定项
% proc a {} { catch {funky} err detail; return $detail }
% a
-code 1 -level 0 -errorstack {INNER {invokeStk1 funky} CALL a} -errorcode NONE -errorinfo {invalid command name "funky"
while executing
"funky"} -errorline 1
%