Tcl 中 errorInfo 中的堆栈跟踪信息
如果在交互模式下使用Tcl,我输入以下内容:
set list {1 2 3 4 5}
set sum 0
foreach el $list {
set sum [expr $sum + $element]
}
它将显示一段非常简洁的信息:
can't read "element": no such variable
但是当我使用
puts $errorInfo
它时将显示:
can't read "element": no such variable
while executing
"expr $sum + $element"
("foreach" body line 2)
invoked from within
"foreach el $list {
set sum [expr $sum + $element]
}"
这才是我真正想要的。
问题是:在非交互模式下,当我想捕获此错误然后放入 errorInfo 来获取堆栈跟踪时,它只会显示简洁的信息。如何获得像上面这样的详细堆栈跟踪?多谢!
编辑添加更多详细信息
说我有以下代码:
proc test1 {} {
set list {1 2 3 4 5}
set sum 0
foreach el $list {
if {[catch {set sum [expr $sum + $element]} err]} {
puts $::errorInfo
}
break
}
}
proc test2 {} {
foreach el $list {
set list {1 2 3 4 5}
set sum 0
set sum [expr $sum + $element]
}
}
#test1
#test2
如果我取消注释“#test1”,它将显示:
can't read "element": no such variable
while executing
"expr $sum + $element"
如果我取消注释“#test2”,它将显示:
can't read "element": no such variable
while executing
"expr $sum + $element"
(procedure "test2" line 5)
invoked from within
"test2"
(file "./test.tcl" line 137)
我想要的是当然test2 的行为。如何使用catch显示此错误信息?
If using Tcl in interactive mode , in which I input the following:
set list {1 2 3 4 5}
set sum 0
foreach el $list {
set sum [expr $sum + $element]
}
it will show a piece of very terse info:
can't read "element": no such variable
but when I use
puts $errorInfo
it wil show:
can't read "element": no such variable
while executing
"expr $sum + $element"
("foreach" body line 2)
invoked from within
"foreach el $list {
set sum [expr $sum + $element]
}"
which is what I really want.
The problem is : in non-interactive mode when I want to catch this error and then puts the errorInfo to get a stack trace, it'll merely display the terse info. How can I get the detailed stack trace like above? Thanks a lot!
Edited to add more details
say I have following code:
proc test1 {} {
set list {1 2 3 4 5}
set sum 0
foreach el $list {
if {[catch {set sum [expr $sum + $element]} err]} {
puts $::errorInfo
}
break
}
}
proc test2 {} {
foreach el $list {
set list {1 2 3 4 5}
set sum 0
set sum [expr $sum + $element]
}
}
#test1
#test2
If I uncomment "#test1", it'll show :
can't read "element": no such variable
while executing
"expr $sum + $element"
if I uncomment "#test2", it'll show:
can't read "element": no such variable
while executing
"expr $sum + $element"
(procedure "test2" line 5)
invoked from within
"test2"
(file "./test.tcl" line 137)
What I want is of course the test2 behavior. How can I display this error info using catch?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您能展示如何在非交互模式下捕获/放置信息吗?
如果您这样做了
那么您所描述的行为是预期的 -
$err
仅具有“简洁信息”。您可能想要puts
相反的是:前缀
::
是必需的,以防您的 catch 在过程或名称空间内调用,以确保您使用的变量是实际的顶层::errorInfo。编辑以解决后续问题
作为Colin回答,在
test1
和test2
中找到的堆栈跟踪因您放置捕获的位置而异。让我举例说明。以下是一些链接的 Tcl 过程:如果您评估,
您将得到如下所示的堆栈跟踪:
这是因为错误发生位置(在
four
内)和捕获错误位置之间的堆栈跟踪,只有一个过程调用。相反,如果您在“更远的地方”捕获了错误,如下所示:
catch
语句和错误之间的堆栈跟踪包括 procsone
、two
、三
和四
。这会产生如下所示的堆栈跟踪:因此...为了匹配您的
test1
示例,如果您按如下方式重新定义三
:并且您评估了
您将不会看到“发现错误”,因为
三
的主体捕获了错误,并打印了堆栈跟踪。堆栈跟踪仅包含catch
语句和错误之间的调用 - 其中(如我的第一个示例)仅包含对four
的调用。因此,放置
catch
语句的位置很重要。与此相关的是,如果您愿意,您可以重新抛出错误,并保留堆栈跟踪。这是
two
的新定义:现在,随着重新抛出的错误,您将看到以下内容:
Can you show how you catch/puts the information in non-interactive mode?
If you did
Then the behavior you described is expected -
$err
only has the "terse info". What you might want toputs
instead is:The prefix
::
is required in case your catch is called inside a proc or namespace to ensure that the variable you use is the actual toplevel ::errorInfo.Edited to address the followup
As Colin answered, the stack traces found in your
test1
andtest2
differ because of where you've placed the catch. Let me illustrate. Here are some chained Tcl procs:If you evaluate
You will just get a stack trace that looks like this:
This is because the stack trace between where the error happened (inside
four
) and where you caught it, there is only one procedure call.If instead you caught the error "further away" like so:
The stack trace between the
catch
statement and the error includes the procsone
,two
,three
, andfour
. This results in a stack trace like so:So... to match your example for
test1
, if you redefinedthree
as follows:And you evaluated
You would not see "Found an error", because the body of
three
caught the error, and printed the stack trace. The stack trace only containing the calls between thecatch
statement and the error - which (like my first example) consists only of the call tofour
.So, where you place your
catch
statements matter.On a related note, you can re-throw the error, preserving the stack trace if you so desire. Here's the new definition for
three
:Now, with the re-thrown error, you would see this:
鉴于您对问题的进一步完善,以下内容可能对您有用:
Given your further refinement of your question, the following may work for you:
这取决于您发现它的位置离错误站点有多近。 errorInfo 中的堆栈跟踪是在堆栈从错误站点展开回第一个封闭捕获或顶层时建立的 - 请参阅 教程。因此,如果您在发生错误的过程中有一个捕获,它就没有机会在 errorInfo 中构建堆栈跟踪。
It depends how close to the error site you catch it. The stack trace in errorInfo is built up as the stack is unwound from the error site back to the first enclosing catch or the top level - see tutorial. So if you have a catch in the proc where the error occurs it doesn't get a chance to build the stack trace in errorInfo.