Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throwing exception vs checking null, for a null argument

What factors dictate throwing an exception if argument is null (eg if (a is null) throw new ArgumentNullException() ), as opposed to checking the argument if it is null beforehand.

I don't see why the exception should be thrown rather than checking for null in the first place? What benefit is there in the throw exception approach?

This is for C#/.NET

Thanks

like image 900
GurdeepS Avatar asked Mar 26 '10 21:03

GurdeepS


3 Answers

Generally you throw ArgumentNullException when an argument gets passed who's value is null but should "never" be null. Its a specific ArgumentException for dealing with nulls.

Oftentimes, if you're aware you're going to get null values as args, you check for null and plan the rest of your method accordingly -- often creating records if none exist, or doing a different subroutine than would've ran if a valid argument was present. In this case, I typically don't use ArgumentNullException because I plan on null being a valid input.

I only use ArgumentNullException when I'm positive something shouldn't be null when it gets there, and I want to make note of an argument thats defined as "invalid".

like image 84
Jason M Avatar answered Nov 09 '22 19:11

Jason M


Your method can do one of three things when an argument is null. It can throw an exception, it can return without doing anything, or it can make assumptions and attempt to continue. I'm assuming you are trying to choose between the first two options.

Since the calling method can always check the arguments prior to calling your method it can prevent invalid values from being passed. This is true regardless of how your method handles invalid values.

When your method is called with invalid arguments it should notify the caller that processing did not continue. You can notify it by throwing an exception or returning an error value. If you check for null and don't return an error value the calling method will assume your method processed with no errors.

How do you want the calling method to handle the case where your method does not process? If passing null is a normal occurrence and should be easily handled by the calling method returning an error on a null argument is acceptable. In this case the calling method either checks the arguments prior or the return value after the choice is up to whoever writes the calling method.

If passing a null is very rare then throw an exception. The calling method can prevent the exception by checking arguments before calling your method just like above. By throwing an exception the call stack can be unwound without having to add lots of code.

Summary:
If an argument set to null is routine and the calling method should be written to handle your function returning without doing anything just return an error value. If a null argument is rare and the calling method can't be expected to handle your method doing nothing throw an exception so the call stack is unwound.

like image 31
Robert Menteer Avatar answered Nov 09 '22 19:11

Robert Menteer


Don't ever write code that checks for a null argument and then does nothing. Hiding bugs in the client code is quite wrong, you want to be as helpful as possible when your API doesn't get used properly.

ArgumentNullException is very helpful.

like image 22
Hans Passant Avatar answered Nov 09 '22 19:11

Hans Passant