如何获取代码行号和错误信息?

How can I get the code line number along with errorinfo?

我正在使用以下 TCL 代码:

proc RunCSM { scen } {
                catch { $scen start }
                if { "[$scen status]" != "SUCCESS" } {
                        puts "$scen FAILED.  Error Info:"
                        puts "[$scen errorInfo]" ...

问题是在这种情况下出现错误,它根据需要显示错误信息调试信息作为 errorInfo 标志的输出,但在这种情况下我还需要失败代码的行号。这怎么可能?

从 8.5 开始,最简单的方法是切换到 catch 的形式,这样您就可以在其 second 脚本后的可选参数:

catch { $scen start } msg opt
if { "[$scen status]" ne "SUCCESS" } {   # use 'ne' to compare strings, please
    set info [dict get $opt -errorinfo]
    set line [dict get $opt -errorline]
    puts "$scen FAILED saying '$msg' at $line. Error Info:"
    puts $info
    # ...
}