Tcl 中 errorInfo 中的堆栈跟踪信息

发布于 2024-08-20 03:19:58 字数 1517 浏览 6 评论 0原文

如果在交互模式下使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴意少年 2024-08-27 03:19:58

您能展示如何在非交互模式下捕获/放置信息吗?

如果您这样做了

if {[catch {...your...code...here...} err]} {
   puts "Error info $err"
}

那么您所描述的行为是预期的 - $err 仅具有“简洁信息”。您可能想要 puts 相反的是:

   puts "Error info $err\nFull info: $::errorInfo"

前缀 :: 是必需的,以防您的 catch 在过程或名称空间内调用,以确保您使用的变量是实际的顶层::errorInfo。

编辑以解决后续问题

作为Colin回答,在 test1test2 中找到的堆栈跟踪因您放置捕获的位置而异。让我举例说明。以下是一些链接的 Tcl 过程:

proc one {} {
  two
}
proc two {} {
  three
}
proc three {} {
  four
}
proc four {} {
  error "Yup, an error"
}

如果您评估,

catch {four}
puts $::errorInfo

您将得到如下所示的堆栈跟踪:

Yup, an error
    while executing
"error "Yup, an error""
    (procedure "four" line 2)
    invoked from within
"four"

这是因为错误发生位置(在 four 内)和捕获错误位置之间的堆栈跟踪,只有一个过程调用。

相反,如果您在“更远的地方”捕获了错误,如下所示:

catch {one}
puts $::errorInfo

catch 语句和错误之间的堆栈跟踪包括 procs onetwo。这会产生如下所示的堆栈跟踪:

Yup, an error
    while executing
"error "Yup, an error""
    (procedure "four" line 2)
    invoked from within
"four"
    (procedure "three" line 2)
    invoked from within
"three"
    (procedure "two" line 2)
    invoked from within
"two"
    (procedure "one" line 2)
    invoked from within
"one"

因此...为了匹配您的 test1 示例,如果您按如下方式重新定义

proc three {} {
  catch {four}
  puts $::errorInfo
}

并且您评估了

if {[catch {one}]} {
   puts "Found an error"
}

您将不会看到“发现错误”,因为的主体捕获了错误,并打印了堆栈跟踪。堆栈跟踪仅包含 catch 语句和错误之间的调用 - 其中(如我的第一个示例)仅包含对 four 的调用。

因此,放置 catch 语句的位置很重要。


与此相关的是,如果您愿意,您可以重新抛出错误,并保留堆栈跟踪。这是 two 的新定义:

proc three {} {
  if {[catch {four} err]} {
    puts "Caught an error $err, re-throwing"
    error $err $::errorInfo
  }
}

现在,随着重新抛出的错误,您将看到以下内容:

tchsh% catch {one}
Caught an error Yup, an error, re-throwing
1
tclsh% set ::errorInfo
Yup, an error
    while executing
"error "Yup, an error""
    (procedure "four" line 2)
    invoked from within
"four"
    (procedure "three" line 2)
    invoked from within
"three"
    (procedure "two" line 2)
    invoked from within
"two"
    (procedure "one" line 2)
    invoked from within
"one"

Can you show how you catch/puts the information in non-interactive mode?

If you did

if {[catch {...your...code...here...} err]} {
   puts "Error info $err"
}

Then the behavior you described is expected - $err only has the "terse info". What you might want to puts instead is:

   puts "Error info $err\nFull info: $::errorInfo"

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 and test2 differ because of where you've placed the catch. Let me illustrate. Here are some chained Tcl procs:

proc one {} {
  two
}
proc two {} {
  three
}
proc three {} {
  four
}
proc four {} {
  error "Yup, an error"
}

If you evaluate

catch {four}
puts $::errorInfo

You will just get a stack trace that looks like this:

Yup, an error
    while executing
"error "Yup, an error""
    (procedure "four" line 2)
    invoked from within
"four"

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:

catch {one}
puts $::errorInfo

The stack trace between the catch statement and the error includes the procs one, two, three, and four. This results in a stack trace like so:

Yup, an error
    while executing
"error "Yup, an error""
    (procedure "four" line 2)
    invoked from within
"four"
    (procedure "three" line 2)
    invoked from within
"three"
    (procedure "two" line 2)
    invoked from within
"two"
    (procedure "one" line 2)
    invoked from within
"one"

So... to match your example for test1, if you redefined three as follows:

proc three {} {
  catch {four}
  puts $::errorInfo
}

And you evaluated

if {[catch {one}]} {
   puts "Found an error"
}

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 the catch statement and the error - which (like my first example) consists only of the call to four.

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:

proc three {} {
  if {[catch {four} err]} {
    puts "Caught an error $err, re-throwing"
    error $err $::errorInfo
  }
}

Now, with the re-thrown error, you would see this:

tchsh% catch {one}
Caught an error Yup, an error, re-throwing
1
tclsh% set ::errorInfo
Yup, an error
    while executing
"error "Yup, an error""
    (procedure "four" line 2)
    invoked from within
"four"
    (procedure "three" line 2)
    invoked from within
"three"
    (procedure "two" line 2)
    invoked from within
"two"
    (procedure "one" line 2)
    invoked from within
"one"
烟若柳尘 2024-08-27 03:19:58

鉴于您对问题的进一步完善,以下内容可能对您有用:

# gets the stack up to the caller
proc get_stack {} {
    set result {}
    for {set i [expr {[info level] -1}]} {$i >0} {incr i -1} {
        lappend result [info level $i]
    }
    return $result
}

# formats the stack for display
proc format_stack {stackList} {
    return "\twhile executing: [join $stackList \n\twhile\ executing:\ ]"
}

# test function that has an error
proc test3 {x y} {
    set list {1 2 3 4 5}
    set sum 0
    foreach el $list {
        if {[catch {set sum [expr $sum + $element]} err]} {
            puts "$::errorInfo\n[format_stack [get_stack]]"
        }
        break 
    }
}

# wrapper test function, just so we can show the call stack more visibly
proc test4 {a b c} { 
    test3 A B
}

# and, we call it and show the output
% test4 1 2 3
can't read "element": no such variable
    while executing
"expr $sum + $element"
    while executing: test3 A B
    while executing: test4 1 2 3

Given your further refinement of your question, the following may work for you:

# gets the stack up to the caller
proc get_stack {} {
    set result {}
    for {set i [expr {[info level] -1}]} {$i >0} {incr i -1} {
        lappend result [info level $i]
    }
    return $result
}

# formats the stack for display
proc format_stack {stackList} {
    return "\twhile executing: [join $stackList \n\twhile\ executing:\ ]"
}

# test function that has an error
proc test3 {x y} {
    set list {1 2 3 4 5}
    set sum 0
    foreach el $list {
        if {[catch {set sum [expr $sum + $element]} err]} {
            puts "$::errorInfo\n[format_stack [get_stack]]"
        }
        break 
    }
}

# wrapper test function, just so we can show the call stack more visibly
proc test4 {a b c} { 
    test3 A B
}

# and, we call it and show the output
% test4 1 2 3
can't read "element": no such variable
    while executing
"expr $sum + $element"
    while executing: test3 A B
    while executing: test4 1 2 3
栀梦 2024-08-27 03:19:58

这取决于您发现它的位置离错误站点有多近。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文