TCL,在catch命令中获取完整的错误消息 [英] TCL, get full error message in catch command
问题描述
#!/usr/bin/tclsh
proc test {} {
aaa
}
test
运行此脚本时出现错误消息:
When I run this script I get error message:
invalid command name "aaa"
while executing
"aaa"
(procedure "test" line 2)
invoked from within
"test"
(file "./a.tcl" line 7)
如果我在 catch
中运行测试命令,只会得到第一行错误消息。
If I run test command in catch
I get only first line of error message.
#!/usr/bin/tclsh
proc test {} {
aaa
}
catch test msg
puts $msg
此打印:
无效命令名称 aaa
在catch命令中是否可以获得完整的错误消息(文件,行,过程)?我的程序有很多文件,并且仅收到一行错误消息,很难从哪里找到它。
Is it possible to get full error message (file, line, procedure) in catch command? My program has many files and by getting just one line of error message it is difficult to find from where is it.
推荐答案
简短的答案是查看 errorInfo
的值,该值将包含堆栈跟踪。
The short answer is to look at the value of errorInfo
which will contain the stack trace.
更完整的答案是查看捕获和返回手册页,并使用 -optionsVarName $
catch
语句的c $ c>参数来收集提供的更详细的信息。 返回
手册页提供了一些有关使用此信息的信息。但是一个交互式会话中的一个粗略示例:
The more complete answer is to look at the catch and the return manual pages and make use of the -optionsVarName
parameter to the catch
statement to collect the more detailed information provided. The return
manual page gives some information on using this. But a rough example from an interactive session:
% 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
%
detail
变量是字典,因此使用 dict get $ detail -errorinfo
可以获取该特定项目。
The detail
variable is a dictionary, so use dict get $detail -errorinfo
to get that particular item.
这篇关于TCL,在catch命令中获取完整的错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!