2011-05-25 45 views

回答

1

您还可以定义一个bgerror程序并调用你的代码作为后台任务:

proc bgerror { msg } { 
    puts "bgerror:$msg" 
    # Cleanup code here 
} 

proc troubleCode { } { 
    adjsad s ahda 
} 

after 1 troubleCode 
4

这是不是很清楚你真的想要什么。

使用catch命令可以捕获任何错误。如果你需要它的顶层工作,你可以catch在评估你的脚本的其余部分,像

catch { 
    source ./the_rest_of_the_code.tcl 
} err 

对于异步程序(那些使用事件循环,TK在内)这不是那么容易,因为意外的错误可以在回调中提升。要处理那些看看bgerror的命令。

+0

正如我们不能增加渔获完整的代码,“bgerror”的方式做一些事情。我需要在TCL中有一些复杂的错误系统。感谢您提供关于bgerror的想法。 – OliveOne 2011-05-25 11:54:27

3

另一种方法是在leavestep模式下使用执行跟踪,该模式允许您测试每个命令是否执行失败并确定发生的情况。 (这非常类似于AOP中某些类型的方面可以做的事情。)

proc doIt {} { 
    namespace eval :: { 
     # Your real code goes in here 
    } 
} 
trace add execution doIt leavestep {::apply {{cmd cmdArgs code result op} { 
    if {$code == 1} {#0 == ok, 1 == error 
     puts "ERROR >>$result<< from $cmdArgs" 
    } 
}}} 
doIt 

虽然这很慢。