Should I Throw ArgumentNullException if a string is blank?

.Net

.Net Problem Overview


I'm working on a method that does something given a string parameter. A valid value for the string parameter is anything other than null or string.Empty. So my code looks like this.

private void SomeMethod(string someArgument)
{
    if(string.IsNullOrEmpty(someArgument))
        throw new ArgumentNullException("someArgument");

    // do some work
}

Nothing too exciting there. My question is, is it okay to throw an ArgumentNullException even if the string is equal to string.Empty? Because technically it isn't null. If you believe it should not throw ArgumentNullException, what exception should be thrown?

.Net Solutions


Solution 1 - .Net

ArgumentException should be thrown for the String.Empty case. This would indicate an issue other than it being null. To avoid a NullReferenceException I check for null first, then I trim and check for the empty case to prevent any whitespace from passing.

private void SomeMethod(string someArgument)
{
    if(someArgument == null)
        throw new ArgumentNullException("someArgument");

    if (someArgument.Trim() == String.Empty)
        throw new ArgumentException("Input cannot be empty", "someArgument");

    // do some work
}

As of .NET 4.0 you can use the String.IsNullOrWhiteSpace method to perform these checks in one go. By doing so you forgo the ability to specify a granular exception type, so I would opt for the ArgumentException and update the message accordingly.

Solution 2 - .Net

You should throw an ArgumentException if an empty string is not an accepted input for your method. It may be very confusing to clients if you throw an ArgumentNullException while they didn't provide a null argument.

It is simply another use case. You may also have methods that do not accept null input values but that do accept empty strings. It's important to be consistent across your entire application.

Solution 3 - .Net

Taking all the things into account that have been said (Joe / Ahmad Mageed), I would create an exception for that case then.

class ArgumentNullOrEmptyException : ArgumentNullException

Solution 4 - .Net

ArgumentNullException is sometimes used in the .NET Framework for the String.IsNullOrEmpty case - an example is System.Windows.Forms.Clipboard.SetText.

So I think it's reasonable to do the same in your code, unless there is some real value in distinguishing the two cases.

Note that this and other exceptions derived from ArgumentException generally indicate a programming error, and therefore need to provide information needed to help a developer diagnose the problem. Personally I think it's unlikely that a developer would find it confusing if you use ArgumentNullException for an empty string argument, especially if you document this behavior as in the example below.

/// <summary>
/// ... description of method ...
/// </summary>
/// <param name="someArgument">... description ...</param>
/// <exception cref="ArgumentNullException">someArgument is a null reference or Empty.</exception>
public void SomeMethod(string someArgument)
{
   ...
}

Solution 5 - .Net

Old question, but since Google ranks it highly, here's another (better!) option for future readers: ArgumentOutOfRangeException.

ArgumentException is the base class for both ArgumentNullException and ArgumentOutOfRangeException, amongst others. That means it's more generic and less informative to developers who are handling the exception. In other words, whilst all ArgumentNullExceptions and ArgumentOutOfRangeExceptions are also ArgumentExceptions, the reverse is not true.

ArgumentOutOfRangeException is a more specific exception which says "the value you provided was not in the range of values I expected", which is exactly what you're trying to tell other developers. It's the best standard exception type if you won't accept an empty string.

Alternatively, create your own exception type derived from ArgumentNullException, if it's really important to distinguish between values.

When handling your exception, other developers who don't need to distinguish why the value they supplied was wrong can just catch ArgumentException and get them all at once.

Solution 6 - .Net

This depends on circumstance really.

The question comes down to, is it really an error? By that I mean do you always expect a value? If you do, then probably your best bet here is creating your own Exception, perhaps like so:

class StringEmptyOrNullException : Exception
{
}

Where you can also add your own constructors and added information etc.

If it however is not an "exceptional" happening in your program, if would probably be a better idea to return null from the method and handle it from there. Just remember, Exception's are for exceptional conditions.

Hope this helps,

Kyle

Solution 7 - .Net

Why don't use this code?

private void SomeMethod(string someArgument)
{
//chek only NULL
if(ReferenceEquals(someArgument,null))
    throw new ArgumentNullException("someArgument");

// and after trim and check
if (someArgument.Trim() == String.Empty)
    throw new ArgumentException("Input cannot be empty", "someArgument");

// do some work
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKepboyView Question on Stackoverflow
Solution 1 - .NetAhmad MageedView Answer on Stackoverflow
Solution 2 - .NetRonald WildenbergView Answer on Stackoverflow
Solution 3 - .NetMarcel JackwerthView Answer on Stackoverflow
Solution 4 - .NetJoeView Answer on Stackoverflow
Solution 5 - .NetMellevsenView Answer on Stackoverflow
Solution 6 - .NetKyle RosendoView Answer on Stackoverflow
Solution 7 - .Net1111View Answer on Stackoverflow