Don
Don

Reputation: 1070

ArgumentNullException for Integer

In .NET, is it more appropriate to throw an argument null exception for an Integer if the value is Integer.MinValue or Integer = 0 (assuming that 0 is not a valid value)?

Upvotes: 1

Views: 884

Answers (3)

bradtgmurray
bradtgmurray

Reputation: 14313

If the argument is not null, don't throw an ArgumentNullException. It would probably be more reasonable to throw an ArgumentException, explained here.

edit: ArgumentOutOfRangeException is probably even better, as suggested above by Avenger546.

Upvotes: 0

Vaibhav
Vaibhav

Reputation: 11436

Well, I think if you are using an int, then it would be better to say InvalidArgumentException.

Alternatively, you could make your INTs nullable by declaring them as int? (especially if you expect null values for your int.)

Upvotes: 2

Adam V
Adam V

Reputation: 6356

Throwing an ArgumentNullException isn't appropriate unless the argument is actually null. Throw an ArgumentOutOfRangeException instead (preferably with a message informing the user what values of int are actually acceptable).

ArgumentOutOfRangeException is thrown when a method is invoked and at least one of the arguments passed to the method is not a null reference (Nothing in Visual Basic) and does not contain a valid value.

Upvotes: 8

Related Questions