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

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

问题描述

我明白异常是例外情况[a],但除了只是重复,并且超过,我从未找到这个事实的一个实际原因。



由于他们停止执行,所以你不希望他们使用简单的条件逻辑,但是为什么不输入验证呢? / p>

说你要循环访问一组输入,并捕获每个异常将它们分组在一起以进行用户通知...我不断看到,这是某种错误的,因为用户一直输入不正确的输入,但这一点似乎是基于语义<​​/a> 。



输入不是预期的,因此是例外。抛出一个异常,我可以确定什么是StringValueTooLong或IntegerValueTooLow或InvalidDateValue或其他错误。为什么这被认为是错误的?



抛出异常的替代方法是返回(并最终收集)错误代码或更差的错误字符串。然后我会直接显示这些错误字符串,或解析错误代码,然后向用户显示相应的错误消息。不会将异常视为可伸缩的错误代码?为什么要创建一个单独的错误代码和消息表,当这些表可以通过我的语言中已经构建的异常功能进行推广?



另外,我 Martin Fowler发现这篇文章,关于如何处理这样的事情 - 通知模式。我不知道我如何看到这是除了不停止执行的异常之外的任何东西。



a:我读过任何关于异常的地方。



---编辑---

已经做出了很多重要的事情。我已经评论过大部分的评论,但是我还没有完全相信。



我不是主张将异常作为适当的手段解决输入验证,但是我想找到很好的理由,为什么这种做法被认为是如此的邪恶似乎最常见的解决方案只是例外的伪装。

解决方案

阅读这些答案,我觉得非常无助,说例外​​只能用于特殊条件。这就提出了什么是特殊条件的整个问题。这是一个主观术语,其最佳定义是您的正常逻辑流程不处理的任何条件。换句话说,一个特殊的条件是处理使用异常的任何条件。



我很好,作为一个定义,我不知道我们会得到任何比这更接近。但是你应该知道这是你正在使用的定义。



如果你在某种情况下要反对例外,你必须解释如何将宇宙条件成为特殊和非特殊。



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


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?

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.

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?

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: Everywhere I've read anything about Exceptions.

--- Edit ---

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天全站免登陆