Tuesday, March 1, 2011

InvalidArgumentException vs OutOfRangeException

When would you use InvalidArgumentException versus OutOfRangeException for parameters to a method? Would you lean more towards OutOfRangeException for a parameter that is not correct (e.g. empty string)?

From stackoverflow
  • I'd use the OutOfRangeException only when working with arrays / collections and a given index is incorrect.

    InvalidArgumentException is more suited to the case of passing an empty string if a non-empty string is required.

    David in Dakota : http://msdn.microsoft.com/en-us/library/system.argumentoutofrangeexception.aspx states that ArgumentOutOfRange is "thrown when the value of an argument is outside the allowable range of values" but doesn't make any mention of array/collection usage.

0 comments:

Post a Comment