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

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

问题描述

根据 .NET Framework 一般参考:错误引发和处理指南 在正常"操作期间不应抛出异常.用户对 Web 表单的无效输入(假设用户输入重复名称)是否正常?<强>!!重要提示!!:我相信我们几乎都对此有意见,请提供可靠来源的参考.

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.

更多背景知识:我正在质疑我正在阅读的一本书所提倡的模型验证方法.这本书建议您在提供要保存的无效数据时从存储库中抛出自定义异常.现在,在我看来这违反了 MS 指导方针,因为您使用异常作为流量控制......除非在正常"操作之外考虑接收无效数据.我只是想看看是否有来自可靠来源的进一步指导来解决这个问题.

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.

推荐答案

一般来说,无效或格式错误的输入不被视为异常",应使用异常以外的其他方法进行处理.但请注意,这是一个准则 - 很可能在某些情况下使用异常来处理问题会产生更好的代码.

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