Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which exception to throw when sequence lengths are not the same

Tags:

c#

exception

I re-implemented Enumerable.Zip (mine is called Merge). It is the same as the original, except if one enumerator's MoveNext returns false before the other (indicating the sequences are not of the same length), it throws an exception.

My question is: What would be the most appropriate exception from the BCL to throw here? (I want this to closely mimic the rest of the Enumerable (LINQ) functions.)

like image 355
Jonathon Reinhart Avatar asked Oct 22 '22 17:10

Jonathon Reinhart


1 Answers

I think that since your second array will be an argument. So

System.ArgumentOutOfRangeException

Subclass of ArgumentException that’s thrown when a (usually numeric) argument is too big or too small. For example, this is thrown when passing a negative number into a function that accepts only positive values.

OR may be

System.InvalidOperationException

Thrown when the state of an object is unsuitable for a method to successfully execute, regardless of any particular argument values. Examples include reading an unopened file or getting the next element from an enumerator where the underlying list has been modified partway through the iteration.

like image 132
Nikhil Agrawal Avatar answered Nov 15 '22 05:11

Nikhil Agrawal