用户输入为空时应该抛出什么异常? [英] What is the exception that should be thrown when user input is blank?
问题描述
我已经搜索了一段时间,但我肯定已经错过了,是否有任何文档说明当值不正确/空白时应该抛出什么?
I've been searching for some time now, and I'm sure I've missed it, is there any documentation that states what should be thrown when a value is incorrect/blank?
例如, Python
具有 ValueError
和文档明确说明了何时使用它.
For example, Python
has ValueError
and the documentation clearly states when to use it.
我有以下方法:
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
}
我搜索了 throw
, 错误
,和 catch
,但是找不到它.
I've searched throw
, error
, and catch
, but couldn't find it.
推荐答案
Tcl没有预定义的异常层次结构.throw命令带有2个参数: type
是单词列表;而 message
是人类的错误消息.
Tcl doesn't have a pre-defined hierarchy of exceptions. The throw command takes 2 arguments: type
is a list of words; and message
is an error message for humans.
您可以做类似的事情
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
}
如果您想捕获该错误:
try {
set answer [getJobinfo "What is the answer to this question"]
} trap {Value *} msg {
puts "Value Error: $msg"
}
throw
和 try
通过 throw
调用的 type
字进行交互.我们抛出空值"或无效值".在陷阱中,我们完全匹配 Value
,但是我们不会完全匹配 *
.事后看来, *
不应存在. try
联机帮助页一开始不是很清楚:
throw
and try
interact via the type
words of the throw
call. We throw "Value Empty" or "Value Invalid". In the trap, we match Value
exactly, but we won't match *
exactly. In hindsight the *
should not be there.
The try
manpage is not super clear at first read:
陷阱 模式 variableList 脚本
如果对 body 的求值导致错误并且解释器的状态词典中的 -errorcode 的前缀等于 pattern <,则此子句匹配/em>.从-错误代码中提取的前缀词的数量等于模式的列表长度,并且在-错误代码和模式进行比较.
This clause matches if the evaluation of body resulted in an error and the prefix of the -errorcode from the interpreter's status dictionary is equal to the pattern. The number of prefix words taken from the -errorcode is equal to the list-length of pattern, and inter-word spaces are normalized in both the -errorcode and pattern before comparison.
模式不是 regexp
或 string match
意义上的模式:它是一个单词列表,每个单词都被一对一地匹配以及尝试 body 中抛出的单词列表.
pattern is not a pattern in the regexp
or string match
sense: it's a list of words that is matched one-by-one with the list of words thrown in the try body.
try
可以通过多个陷阱实现,以具有级联的陷阱":
The try
can be implemented with multiple traps to have cascading "catches":
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
}
这篇关于用户输入为空时应该抛出什么异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!