문제

I am a bit confused.

What MSDN says for ArgumentException is:

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

So I would throw an exception if the passed arguments to my method are not valid.

But also I found out that it is suggested to return boolean plus a string for reason. -instead of throwing an exception.

So,

Question : When validationg method parameters, and when the parameters are not valid, when should the Validation method throw an exception and when it should not?

도움이 되었습니까?

해결책

If you have a method whose purpose it is to validate its arguments (for example: is this a valid username/password combination), by all means return a bool (false = invalid) plus a reason. Do not throw an exception for an expected situation (if it's not expected, why validate?)

The situation is different if you have a "Process" method: there you must have valid parameters, so it is reasonable to throw an exception (stating the problem) when they are invalid.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top