0

just seeking some advice here for the sake of best practices.

I have a boolean method that checks for a certain condition for each element of an array. The array parameter cannot be null nor contains null values, otherwise it wouldn't make sense to return either true or false.

Best practices states that in the event of an invalid argument being passed to a method, an ArgumentException (or derived) should be thrown. The method will throw a ArgumentNullException if the array parameter is null. However, I'm not so sure on what I should throw for an empty array or an array that contains null values.

My initial thought was to throw a plain ArgumentException with a message explaining the nature of the problem, but a colleague suggested that I'd throw an ArgumentOutOfRangeException instead.

I usually think of ArgumentOutOfRangeException as something that says "too high" or "too low". My colleague seems to believe it can also stand for "not enough" and "something missing".

Is he right? Or should I follow my first idea and throw ArgumentException?

Crono
  • 10,211
  • 6
  • 43
  • 75
  • Argument null is just that and only that. An empty array or an an array with null values is not a null argument in my mind. – paparazzo Jul 23 '13 at 20:08
  • Neither in mine. This is why I'm throwing ArgumentNullException ONLY if the array itself is a null value. The question is what should I throw when the array is NOT null but still invalid. – Crono Jul 23 '13 at 20:22
  • If the array is NOT null and invalid then how is that not an ArgumentOutOfRangeException? – paparazzo Jul 23 '13 at 20:25
  • I don't know, that's kind of the question here! :) Should it be an ArgumentOutOfRangeException or it's parent type ArgumentException? I feel as if it could be both. What makes me hesitate is that ArgumentException isn't very specific, whereas ArgumentOutOfRangeException sounds as if it was thought for a parameter expected to fall into a predetermined range, like @Taras mentionned down below. – Crono Jul 24 '13 at 12:27

1 Answers1

1

MSDN states that you should throw ArgumentOutOfRangeException for cases where arguments are NOT null. So that is not an appropriate exception type to throw here.

You mentioned that you also want to check the array for being empty. If you do that, then throwing ArgumentNullException is not appropriate either.

That leaves you with ArgumentException.

Taras Dzyoba
  • 121
  • 3
  • I'm not sure I understand your first paragraph. If, indeed, the ArgumentOutOfRangeException is for cases where arguments are NOT null, then wouldn't that make it the right exception type to throw in a scenario where the array parameter isn't null BUT contains invalid values? – Crono Jul 23 '13 at 19:39
  • In the first paragraph I talked about the situation where one of the array elements is null. The way I understood your question is that you want to have one check to verify if the paramater is null and throw an ArgumentNullException. Then you would check if any elements are null or string.empty and throw another exception for these cases. It makes most sense to me to throw a generic ArgumentException since both ArgumentNullException and ArgumentOutOfRangeException are not the right types to throw in both cases. – Taras Dzyoba Jul 23 '13 at 20:42
  • 1
    Also even if the null check was not there, I think that throwing ArgumentOutOfRangeException for empty string in not right. It assumes that there is a range of values you are checking against, where you are actually just checking against one value. I did a quick test of .net assemblies: System.Net.Mail.MailAddress mailaddress = new System.Net.Mail.MailAddress("") throws ArgumentException. – Taras Dzyoba Jul 23 '13 at 20:46
  • You make a valid point for ArgumentOutOfRangeException. I, too, feels that it isn't the right candidate. MailAddress constructor is a convincing demonstration too. However, I'm still certain that throwing an ArgumentNullException if the parameter is indeed null is the right thing to do. This is how the MailAddress constructor works: like you said it will throw a generic ArgumentException class on String.Empty, but on a null value it will throw an ArgumentNullException. ;) If you edit that part of your original comment I'll mark it as answer. Thanks a lot for your help! – Crono Jul 24 '13 at 12:36