error-handling - 如何找出 tcl 命令可能生成哪些错误?

标签 error-handling tcl

tcl try manpage ,它有以下示例:

try {
    set f [open /some/file/name w]
} trap {POSIX EISDIR} {} {
    puts "failed to open /some/file/name: it's a directory"
} trap {POSIX ENOENT} {} {
    puts "failed to open /some/file/name: it doesn't exist"
}

那太好了,它有效,但是我如何发现 {POSIX ENOENT}open 的可能陷阱模式? open manpage没有提到它。对于tcl中给定的任意命令,如何找出可能的错误是什么?

最佳答案

try {} trap {} 用于当存在需要捕获的特定错误时。 对于更一般的陷阱,请使用try {} on error {}

try {
   set fh [open myfile.txt w]
} on error {err res} {
   puts "Error on open: $res"
}

还有catch命令:

if { [catch {set fh [open myfile.txt w]}] } {
   puts "error on open."
}

引用文献:try catch

关于error-handling - 如何找出 tcl 命令可能生成哪些错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54247244/

相关文章:

string - TCL-如何在 tcl 程序中将字符串解释为多个参数?

python-3.x - Python 库/包代码在库外工作,但尝试在库内运行会导致导入错误

error-handling - 构建CakePHP注册页面

matlab - 抑制错误消息回溯

c - 在 TCL 8.4 中,如果 #define NULL 0,TclLib 函数 Tcl_SetHashValue 是否允许值为 NULL?

TCL:递归搜索子目录以获取所有 .tcl 文件

c# - 使用 ErrorController 而不是直接 View 处理错误

php - 使用OOP PHP制作登录屏幕

regex - 如何在正则表达式中使用变量(TCL/Expect)

string - 如何在 TCL 中将字符串拆分为单词列表,忽略多个空格?