为什么.NET的StringValidator的验证方法抛出一个异常时,它不会成功? [英] Why does .NET's StringValidator's Validate method throws an exception when it doesn't succeed?

查看:150
本文介绍了为什么.NET的StringValidator的验证方法抛出一个异常时,它不会成功?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如你可以在 MSDN StringValidator文档验证方法的返回值无效
如果验证不成功的验证方法抛出的ArgumentException
我想,你只有在一些特殊情况发生抛出一个异常。
当然,一个验证失败验证也不例外..
为什么不返回布尔?什么我在这里丢失?
这是一个作风问题(即,如果它是返回布尔,它仍然是正确的,只是不同的风格)?

As you can see in the MSDN StringValidator documentation, the Validate method returns void.
If validation doesn't succeed the Validate method throws ArgumentException.
I thought that "You only throw an exception when something exceptional happens".
Surely a validator that failed to validate isn't exceptional..
Why not return bool? What am I missing here?
Is this a "style issue" (i.e. if it was returning bool, it would still be correct, but just different style)?

注:方法 CanValidate 可以有什么用它做,但我仍然认为没有理由对这种行为。

Note: Method CanValidate may have something to do with it, but I still see no reason for this behavior.

推荐答案

看的的ArgumentException类

您可以使用布尔更换信息的ArgumentExeption可能包含哪些内容? 而基于此页面上,

Can you use bool to replace the info that ArgumentExeption may contain? And based on this page,

ArgumentException的:不同的是   被抛出时的论据之一   提供的方法是无效的。

ArgumentException: "The exception that is thrown when one of the arguments provided to a method is not valid.

因此​​,如果在 StringValidator.Validate参数对象(对象)是无效的,应该是最好的选择?返回一个品种全的对象或者只是抛出ArgumentException的?

So if the argument "Object" in StringValidator.Validate(Object) is not valid, what should be the best choice? Return a whole variety of objects or just throw ArgumentException?

这篇关于为什么.NET的StringValidator的验证方法抛出一个异常时,它不会成功?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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