我有以下程序:
proc show_information {args} {
set mandatory_args {
-tftp_server
-device ANY
-interface ANY
}
set optional_args {
-vlan ANY
}
parse_dashed_args -args $args -optional_args $optional_args -mandatory_args $mandatory_args
log -info "Logged in device is: $device"
log -info "Executing proc to get all data"
set commands {
"show mpls ldp neighbor"
"show mpls discovery vpn"
"show mpls interface"
"show mpls ip binding"
"show running-config"
"show policy-map interface $intr vlan $vlan input"
"show run interface $intr
}
foreach command $commands {
set show [$device exec $command]
ats_log -info $show
}
}
我是 tcl 的新手,想知道如果无论如何我们传递了错误的参数或它出错了,我们如何处理错误。在 TCL 中,像 python try *(executes the proc) 和 except *(在失败的情况下打印一些 msg) 之类的东西。
经过一些谷歌搜索“捕获”是 TCL 中使用的,但无法弄清楚如何使用它。
该catch
命令运行脚本并捕获其中的任何故障。命令的结果实际上是一个描述是否发生错误的布尔值(它实际上是一个结果代码,但 0 表示成功,1 表示错误;还有一些其他的,但您通常不会遇到它们)。您还可以给出一个变量,将“结果”放入其中,即成功时的正常结果和失败时的错误消息。
set code [catch {
DoSomethingThat mightFail here
} result]
if {$code == 0} {
puts "Result was $result"
} elseif {$code == 1} {
puts "Error happened with message: $result"
} else {
# Consult the manual for the other cases if you care
puts "Code: $code\nResult:$result"
}
在许多简单的情况下,您可以将其缩短为:
if {[catch {
DoSomethingThat mightFail here
} result]} {
# Handle the error
} else {
# Use the result
}
不过,Tcl 8.6 添加了一个用于处理错误的新命令。该try
命令更像您使用 Python 时所使用的命令:
try {
DoSomethingThat mightFail here
} on error {msg} {
# Handle the error
}
它还支持分别用于保证操作和更复杂的错误处理的finally
andtrap
子句。例如(用 写起来很烦人的东西catch
):
try {
DoSomethingThat mightFail here
} trap POSIX {msg} {
# Handle an error from the operating system
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句