无效用户输入抛出异常的正当理由? [英] Is invalid user input a valid reason for throwing an exception?

查看:115
本文介绍了无效用户输入抛出异常的正当理由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照 .NET Framework常规参考:错误提高和处理原则异常不应在正常的操作被抛出。无效用户输入到网页形式,比如用户输入重复的名字,认为是正常的吗? !注意!!:我敢肯定,我们pretty的多少,都对这个意见,请包括引用可靠消息

According to the .NET Framework General Reference: Error Raising and Handling Guidelines exceptions should not be thrown during 'normal' operations. Is invalid user input to a web form, say the user enters a duplicate name, considered normal? !! IMPORTANT !!: I'm sure we pretty much all have an opinion on this, please include a reference to a reliable source.

编辑:

一个多一点背景:我质疑approch通过一本书我读倡导的模型验证。这本书建议你从一个仓库时无效数据保存提供抛出一个自定义异常。现在,这似乎给我,因为你正在使用的异常流量控制违反MS guidlines ......除非recieving无效数据是认为正常的操作之外。我只是想看看是否有来自可靠来源的任何进一步的指导,以解决这个问题。

A little more background: I'm questioning the approch to model validation advocated by a book I'm reading. The book is suggesting that you throw a custom exception from a repository when provided with invalid data to save. Now, this seems to me to violate MS guidlines because you are using exceptions as flow control...unless recieving invalid data is consider outside of 'normal' operation. I just want to see if there is any further guidance from a reliable source to resolve this.

另一个编辑:

确定这样的两年半以后,我要搬到这个仓库到WCF服务,使用这种方法的例外原来是一个坏主意。哦。

OK so two and a half years later, I'm moving this repository to a WCF service and using exceptions in this method turned out to be a bad idea. Oh well.

推荐答案

一般而言,无效或不当形成的输入没有考虑'例外',应该使用的东西比其他的异常处理。但是请注意,这是一个指导原则 - 有可能是情况下,使用异常处理的问题将得到更好的code

Generally speaking, invalid or ill-formed input is not consider 'exceptional' and should be handled using something other than exceptions. But note that this is a guideline - there may well be situations where using exceptions to handle the problem would result in better code.

这篇关于无效用户输入抛出异常的正当理由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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