NOTE: This is different than the proposed duplicates as this deals with an argument rather than a value. The behavior and applicable scenarios are essentially different.
InvalidArgumentException. when user pass some invalid value or null value when value value is required, it is recommended to handle InvalidArgumentException .
I'd throw the InvalidEnumArgumentException as it will give more detailed information in this case, you are checking on an enum
ArgumentException
looks the most correct to me in this instance (though is not defined in the BCL).
There is a specialized exception for enum arguments - InvalidEnumArgumentException:
The exception thrown when using invalid arguments that are enumerators.
An alternative is ArgumentOutOfRangeException:
The exception that is thrown when the value of an argument is outside the allowable range of values as defined by the invoked method.
The logic for using these is that the passed in argument (value
) is not valid as far as someFunc
is concerned.
Since you have the login in a function you can throw InvalidArgumentException.
The exception that is raised when a parameter that is not valid is passed to a method on the referenced connection to the server.
EDIT:
A better alternative would be: ArgumentException, since InvalidArgumentException
in Microsoft.SqlServer.Management.Common
namespace. Something like:
throw new ArgumentException("Unhandled value: " + value.ToString());
If you were using Code Contracts (something I HIGHLY recommend), you would put this at the start of the method:
Contract.Requires(value == SomeEnum.One || value == SomeEnum.Two);
If you want to check a range of an enum which has too many individual values to write them all explicitly, you can do it like this:
Contract.Requires(SomeEnum.One <= value && value <= SomeEnum.Two);