为什么.NET的StringValidator的验证方法抛出一个异常时,它不会成功? [英] Why does .NET's StringValidator's Validate method throws an exception when it doesn't succeed?
问题描述
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.
推荐答案
您可以使用布尔更换信息的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屋!