Should I Throw ArgumentNullException if a string is blank?

前端 未结 7 1617
独厮守ぢ
独厮守ぢ 2020-12-14 00:11

I 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 li

相关标签:
7条回答
  • 2020-12-14 00:14

    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.

    0 讨论(0)
  • 2020-12-14 00:21

    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
    }
    
    0 讨论(0)
  • 2020-12-14 00:24

    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.

    0 讨论(0)
  • 2020-12-14 00:24

    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

    0 讨论(0)
  • 2020-12-14 00:26

    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.

    0 讨论(0)
  • 2020-12-14 00:37

    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)
    {
       ...
    }
    
    0 讨论(0)
提交回复
热议问题