为什么.NET的StringValidator的验证方法抛出一个异常时,它不会成功?抛出、异常、方法、NET

2023-09-04 03:10:17 作者:享受人生

正如你可以在 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?