throwing an exception if an object is null

20,129

Solution 1

I don't know why you would..

public Exception GetException(object instance)
{
    return (instance == null) ? new ArgumentNullException() : new ArgumentException();
}

public void Main()
{
    object something = null;
    throw GetException(something);
}

Solution 2

Yes, as of C# 7 you can use Throw Expressions

var firstName = name ?? throw new ArgumentNullException("Mandatory parameter", nameof(name));

Source

Solution 3

There is no similar fashion syntax in C# 6.

However, if you want you can simplify null check by using an extension methods...

 public static void ThrowIfNull(this object obj)
    {
       if (obj == null)
            throw new Exception();
    }

usage

foo.ThrowIfNull();

Or improvement it to display null object name.

 public static void ThrowIfNull(this object obj, string objName)
 {
    if (obj == null)
         throw new Exception(string.Format("{0} is null.", objName));
 }

foo.ThrowIfNull("foo");

Solution 4

As of .NET 6 framework you can use the following ThrowIfNull static Method:

void HelloWorld(string argumentOne)
{
    ArgumentNullException.ThrowIfNull(argumentOne);
    Console.WriteLine($"Hello {argumentOne}");
}

Solution 5

If null then null; if not then dot

Code using the null conditional can be easily understood by saying that statement to yourself when reading it. So for instance in your example, if foo is null, then it would return null. If it were not null, then it would "dot" and then throw an exception which I don't believe is what you want.

If you're looking for a shorthand way to handle null checks, I would recommend Jon Skeet's answer here and his related blog post on the topic.

Deborah Kurata referenced this saying in this Pluralsight course which I recommend also.

Share:
20,129
Alex Gordon
Author by

Alex Gordon

Check out my YouTube channel with videos on Azure development.

Updated on February 10, 2022

Comments

  • Alex Gordon
    Alex Gordon about 2 years

    I've recently discovered that:

    if (Foo() != null)    
       { mymethod(); }
    

    can be rewritten as

    Foo?.mymethod()
    

    Can the following be rewritten in a similar fashion?

    if (Foo == null)
    { throw new Exception()}
    
  • Eric Yeoman
    Eric Yeoman over 7 years
    The method call can be changed to: foo.ThrowIfNull(nameof(foo)); Makes it safe if the variable name is changed :)
  • tina Miller
    tina Miller almost 4 years
    Nice, but your arguments are backwards in ArgumentException :)
  • tina Miller
    tina Miller almost 4 years
    That said, they'd be the right way round for ArgumentNullException.
  • Jack Miller
    Jack Miller almost 4 years
    Do not link to pages that are protected. What is the code you are referring to?
  • guneysus
    guneysus about 2 years
    This method introduced with .NET 6 Framework, not C# Lang 10. docs.microsoft.com/en-us/dotnet/api/… For example, if your project netcoreapp3.1 framework and using C# 10.0 you can not use this static method.