6

Let's say I have a method:

public void SayHello(User user)
{
    if (user == null)
        throw new ArgumentNullException(nameof(user));

    Console.Write(string.Format("Hello from {0}", user.Name));
}

It's clear that I should use ArgumentNullException as it shown above to validate that user is not null. Now how can I validate that user.Name is not empty? Would it be a good practice to do like that:

if (string.IsNullOrWhiteSpace(user.Name))
    throw new ArgumentNullException("user", "Username is empty");
Andrei
  • 42,814
  • 35
  • 154
  • 218
  • 1
    If you place that code after the `user == null` check, then sure: that's perfectly fine. You already have the explicit guarantee at that point that `user` is not `null` – Jeroen Vannevel Dec 29 '14 at 13:29
  • 1
    @JeroenVannevel that's clear. I'm just wondering is that a good practice to use ArgumentNullException that way. Should I consider any other exceptions for this? – Andrei Dec 29 '14 at 13:31
  • I'd say you should not do that, and instead prefer the more robust approach, i.e., preventing a User with an empty username to begin with. You could make it so that the programmer should specify the username for all constructors of the `User` object, and throw the exception there. Then, you could expose the `UserName` property with a get only. – julealgon Dec 29 '14 at 13:32
  • 1
    If you're checking whether or not the argument is null then `ArgumentNullException` is as to the point as it will get. This exception has been added exactly for this purpose. Note that you might want to look at the difference between `ArgumentException` and `ArgumentNullException`. – Jeroen Vannevel Dec 29 '14 at 13:32
  • Duplicate: https://stackoverflow.com/questions/15099218/use-of-argumentnullexception-when-accessing-arguments-properties – nawfal Jul 26 '20 at 01:23

2 Answers2

11

No you shouldn't throw ArgumentNullException for this purpose as it is designed specifically to address null references.

The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method that does not accept it as a valid argument.

Instead you should be using ArgumentException or your own subclass of it. let's say InvalidUserException or something similar.

Even msdn talks about.

ArgumentNullException behaves identically to ArgumentException. It is provided so that application code can differentiate between exceptions caused by null arguments and exceptions caused by arguments that are not null. For errors caused by arguments that are not null

So it is clear that if argument is null use ArgumentNullException and ArgumentException otherwise.

That said, Ideally you shouldn't even allow someone to create an instance of User with invalid username. I'd really design my User class in such a way that it can never contain Name as null.

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
5

You should throw ArgumentException if the argument is empty, ArgumentNullException should only be used for null arguments:

if (string.IsNullOrWhiteSpace(user.Name))
    throw new ArgumentException("Username is empty", "user");
Lee
  • 142,018
  • 20
  • 234
  • 287