无效的用户输入是否引发异常的有效理由? [英] Is invalid user input a valid reason for throwing an exception?

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

问题描述

根据 .NET Framework常规参考:错误正常操作中不应抛出异常。是无效的用户输入到Web表单,说用户输入重复名称,认为正常吗?的 !!重要的是:我相信我们几乎都有意见,请参考可靠来源。



编辑: p>

多一点背景:我在质疑我正在阅读的一本书提倡的模型验证。这本书建议您在提供无效数据保存时,从存储库中抛出一个自定义异常。现在,这似乎违反了MS指导原则,因为您使用异常作为流量控制,除非收到无效数据被认为在正常操作之外。我只想看看是否有可靠的来源解决这个问题的进一步指导。



另一个编辑:



确定如此 两年半以后 ,我将该存储库移动到WCF服务,并且使用此方法中的异常被证明是一个坏主意。一般来说,无效或不合格的输入不被认为是特殊的,应该使用其他的东西来处理。比例。但请注意,这是一个指南 - 可能会出现使用异常来处理问题的情况会导致更好的代码。


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.

EDIT:

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.

ANOTHER EDIT:

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.

解决方案

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