tags:

views:

109

answers:

3

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)?

+2  A: 

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.)

Vaibhav
+6  A: 

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.

Adam V
A: 

If the argument is not null, don't throw an ArgumentNullException. It would probably be more reasonable to throw an ArgumentException, explained here http://msdn.microsoft.com/en-us/library/system.argumentexception.aspx.

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

bradtgmurray