Error handling 当用户输入为空时,应该引发什么异常?

Error handling 当用户输入为空时,应该引发什么异常?,error-handling,try-catch,tcl,throw,Error Handling,Try Catch,Tcl,Throw,我已经搜索了一段时间了,我肯定我错过了,有没有文档说明当值不正确/为空时应该抛出什么 例如,Python,文档中明确说明了何时使用它 我有以下方法: proc getJobinfo {question} { puts -nonewline "$question: " flush stdout gets stdin answer set cleanedanswer [string trim [string totitle $answer]] if {$cle

我已经搜索了一段时间了,我肯定我错过了,有没有文档说明当值不正确/为空时应该抛出什么

例如,
Python
,文档中明确说明了何时使用它

我有以下方法:

proc getJobinfo {question} {
    puts -nonewline "$question: "
    flush stdout
    gets stdin answer
    set cleanedanswer [string trim [string totitle $answer]]
    if {$cleanedanswer eq ""} {
       # What error should be thrown?
    }
    return $cleanedanswer
}

我已经找过了,但是找不到

Tcl没有预定义的异常层次结构。throw命令有两个参数:
type
是一个单词列表;而
消息
是针对人类的错误消息

你可以这样做

proc getJobinfo {question} {
    ...
    if {$cleanedanswer eq ""} {
        throw {Value Empty} "Please provide a suitable answer."
    } elseif {[string length $cleanedanswer] < 5} {
        throw {Value Invalid} "Your answer is too short."
    } else ...
    return $cleanedanswer
}

throw
try
通过
输入
throw
调用的单词进行交互。我们抛出“值为空”或“值无效”。在陷阱中,我们精确匹配
,但不会精确匹配
*
。事后看来,
*
不应该存在。 手册页在第一次阅读时不是非常清晰:

陷阱模式变量列表脚本

如果对body的求值导致错误,并且来自解释器状态字典的-errorcode前缀等于模式,则该子句匹配。从-errorcode中提取的前缀词的数量等于模式的列表长度,并且在比较之前,-errorcode和模式中的词间空间都是标准化的

模式不是
regexp
string match
意义上的模式:它是一个单词列表,与try正文中抛出的单词列表一一匹配

try
可以使用多个陷阱来实现,以实现级联“捕获”:


谢谢你的帮助。
*
陷阱中的使用是否在任何地方都有记录?有一点层次结构,其中
POSIX
是用于来自操作系统的前缀,而
TCL
是用于源自TCL实现的错误。除此之外,世界对代码作者是开放的。我想如果你在写一个包,最好先用你的包名?@donal,有没有一个完整的“核心”错误代码列表?
try {
    set answer [getJobinfo "What is the answer to this question"]
} trap {Value *} msg {
    puts "Value Error: $msg"
}
try {
    set answer [getJobinfo "What is the answer to this question"]

} trap {Value Empty} msg {
    do something specific here

} trap {Value Invalid} msg {
    do something specific here

} trap {Value} msg {
    do something general for some other "throw {Value anything} msg"

} on error e {
    this can be default catch-all for any other error

} finally {
    any cleanup code goes here
}