For the first case, I'll pile on and say that ArgumentNullException
is correct.
For the second case, I'm really very surprised that nobody else has said this: You should be making your own Exception
class for that. None of the built-in system exceptions are really appropriate:
ArgumentException
implies that the argument itself was invalid in some way; that's not really the case here. The argument was fine, it's just that something unexpected happened later.
InvalidOperationException
is almost correct, but that exception is generally interpreted to mean that an operation was invoked at the wrong time, such as trying to execute a command on a connection that hasn't been opened yet. In other words, it indicates a mismatch between the current state of the object and the specific operation you tried to perform; that's really not applicable here either.
NullReferenceException
is right out. That's a reserved exception that means something completely different (that the program actually tried to deference the null reference).
None of these are right. What you really need to be doing is communicated specifically what went wrong, and in order to do that, you should create a MissingThingException
. That exception can include the ID of the thing (presumably arg
) in its message/detail. This is the best for callers, because it allows them to catch the specific exception if they know how to handle it, and also the best for end users, because it allows you to leave a meaningful error message.
Summary: Create a custom exception class for this.