为什么说异常对输入验证如此不利? [英] Why are Exceptions said to be so bad for Input Validation?

查看:20
本文介绍了为什么说异常对输入验证如此不利?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解例外是针对特殊情况的"[a],但除了重复之外 over再次,我从来没有发现这一事实的实际原因.

I understand that "Exceptions are for exceptional cases" [a], but besides just being repeated over and over again, I've never found an actual reason for this fact.

由于它们会停止执行,因此您不希望它们用于简单的条件逻辑是有道理的,但为什么不进行输入验证?

Being that they halt execution, it makes sense that you wouldn't want them for plain conditional logic, but why not input validation?

假设您要遍历一组输入并捕获每个异常以将它们组合在一起以供用户通知......我不断发现这在某种程度上是错误的",因为用户一直输入不正确的输入,但这一点似乎基于语义<​​/a>.

Say you were to loop through a group of inputs and catch each exception to group them together for user notification... I continually see that this is somehow "wrong" because users enter incorrect input all the time, but that point seems to be based on semantics.

输入不是预期的,因此是异常的.抛出异常允许我准确定义错误,例如 StringValueTooLong 或 IntegerValueTooLow 或 InvalidDateValue 或其他什么.为什么这被认为是错误的?

The input is Not what was expected and hence is exceptional. Throwing an exception allows me to define exactly what was wrong like StringValueTooLong or or IntegerValueTooLow or InvalidDateValue or whatever. Why is this considered wrong?

抛出异常的替代方法是返回(并最终收集)错误代码或更糟糕的错误字符串.然后我要么直接显示这些错误字符串,要么解析错误代码,然后向用户显示相应的错误消息.异常不会被视为可延展的错误代码吗?为什么要创建一个单独的错误代码和消息表,因为它们可以通过我的语言中已经内置的异常功能进行泛化?

Alternatives to throwing an exception would be to either return (and eventually collect) an error code or far worse an error string. Then I would either show those error strings directly, or parse the error codes and then show corresponding error messages to the user. Wouldn't a exception be considered a malleable error code? Why create a separate table of error codes and messages, when these could be generalized with the exception functionality already built into my language?

此外,我找到了 Martin Fowler 撰写的这篇文章,了解如何处理此类事情- 通知模式.除了不停止执行的异常之外,我不确定我如何看待它.

Also, I found this article by Martin Fowler as to how to handle such things - the Notification pattern. I'm not sure how I see this as being anything other than Exceptions that don't halt execution.

a:我在任何地方都读到过关于异常的任何内容.

a: Everywhere I've read anything about Exceptions.

--- 编辑---

已经提出了许多重要的观点.我已经评论了大部分内容,并对优点进行了 + 赞,但我还没有完全相信.

Many great points have been made. I've commented on most and +'d the good points, but I'm not yet completely convinced.

我并不是要提倡将异常作为解决输入验证的正确方法,但我想找到很好的理由,为什么这种做法被认为如此邪恶,因为似乎大多数替代解决方案都只是伪装的异常.

I don't mean to advocate Exceptions as the proper means to resolve Input Validation, but I would like to find good reasons why the practice is considered so evil when it seems most alternate solutions are just Exceptions in disguise.

推荐答案

阅读这些答案后,我发现说异常应该仅用于异常情况"非常没有帮助.这就引出了什么是特殊情况"的整个问题.这是一个主观术语,其最佳定义是您的正常逻辑流程不处理的任何条件".换句话说,异常条件是您使用异常处理的任何条件.

Reading these answers, I find it very unhelpful to say, "Exceptions should only be used for exceptional conditions". This begs the whole question of what is an "exceptional condition". This is a subjective term, the best definition of which is "any condition that your normal logic flow doesn't deal with". In other words, an exceptional condition is any condition you deal with using exceptions.

我对这个定义没意见,我不知道我们会比这更接近.但你应该知道这是你使用的定义.

I'm fine with that as a definition, I don't know that we'll get any closer than that anyway. But you should know that that's the definition you are using.

如果你要在某种情况下反对例外,你必须解释如何将条件范围划分为例外"和非例外".

If you are going to argue against exceptions in a certain case, you have to explain how to divide the universe of conditions into "exceptional" and "non-exceptional".

在某些方面,它类似于回答程序之间的界限在哪里?"这个问题.答案是,无论你把开始和结束放在哪里",然后我们可以讨论经验法则和不同的风格来确定把它们放在哪里.没有硬性规定.

In some ways, it's similar to answering the question, "where are the boundaries between procedures?" The answer is, "Wherever you put the begin and end", and then we can talk about rules of thumb and different styles for determining where to put them. There are no hard and fast rules.

这篇关于为什么说异常对输入验证如此不利?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆