Frage

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?

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top