Search code examples
.netoop

Should Guid.Empty Result in ArgumentException or ArgumentOutOfRangeException


Imagine that you have a method with the following signature:

public void DoSomething(Guid id)

If Guid.Emtpy represents an illegal value, which Exception type would it be most appropriate to throw? ArgumentException or ArgumentOutOfRangeException?

I'm leaning slightly towards ArgumentException, since I don't think that all Guids except Guid.Empty is much of a range - it's a bit too inclusive: There is only one excluded member.

However, I am in no way determined that this should be the case, so I'd like to hear if anyone can provide arguments for one or the other?

I'm well aware that this is mainly a semantic discussion, but in the interest of good API design I'd still like to know whether there are clear cases for one or the other option.


Solution

  • Saying that an empty value is out of range just seems wrong. As Guid.Empty is usually used when there is no defined value at all, there isn't really any value that can be outside of the range.

    The situation is similar to when you use ArgumentNullException, except there is no specific exception for empty values. An ArgumentNullException would be misleading, so ArgumentException is a better fit.

    Also consider if ApplicationException would be better. That's used for exceptions in the application rather than a class library.