代码之家  ›  专栏  ›  技术社区  ›  Adam Lear

字符串参数的ArgumentException或ArgumentNullException?

  •  32
  • Adam Lear  · 技术社区  · 16 年前

    就最佳实践而言,哪一个更好:

    public void SomeMethod(string str) 
    {
        if(string.IsNullOrEmpty(str))
        {
            throw new ArgumentException("str cannot be null or empty.");
        }
    
        // do other stuff
    }
    

    public void SomeMethod(string str) 
    {
        if(str == null) 
        {
            throw new ArgumentNullException("str");
        }
    
        if(str == string.Empty)
        {
            throw new ArgumentException("str cannot be empty.");
        }
    
        // do other stuff
    }
    

    4 回复  |  直到 16 年前
        1
  •  39
  •   Jeffrey L Whitledge    15 年前

    我要说的是,第二种方法确实更精确——是的,它更麻烦,但是你可以用一种方法来包装它,以避免一直这样做。它甚至可以是一种扩展方法:

    str.ThrowIfNullOrEmpty("str");
    
    
    public static void ThrowIfNullOrEmpty(this string value, string name)
    {
        if (value == null)
        {
            throw new ArgumentNullException(name);
        }
        if (value == "")
        {
            throw new ArgumentException("Argument must not be the empty string.",
                                        name);
        }
    }
    

    另一种可能有用的形式是,如果一切正常,则返回原始字符串。你可以这样写:

    public Person(string name)
    {
        this.name = name.CheckNotEmpty();
    }
    

    Code Contracts 作为抛出自己的异常的替代方法。

        2
  •  5
  •   Andrew Bezzub    16 年前

        3
  •  1
  •   sommmen    7 年前

    在空字符串上抛出null引用可能会非常混乱-字符串的默认值为null,表示未初始化的字符串,而和空字符串可能会构成其他问题。

    相反,您可以抛出以下类:

     public class ArgumentNullOrEmptyException : ArgumentNullException
    {
        #region Properties And Fields
    
        private static string DefaultMessage => $"A value cannot be null or empty{Environment.NewLine}";
    
        #endregion
    
        #region Construction and Destruction
    
        public ArgumentNullOrEmptyException()
            : base(DefaultMessage)
        {
        }
    
        public ArgumentNullOrEmptyException(string paramName)
            : base(paramName, 
                      $"{DefaultMessage}" +
                      $"Parameter name: {paramName}")
        {
        }
    
        public ArgumentNullOrEmptyException(string message, Exception innerException)
            : base($"{DefaultMessage}{message}", innerException)
        {
        }
    
        public ArgumentNullOrEmptyException(string paramName, string message)
            : base(paramName, 
                  $"{DefaultMessage}" +
                  $"Parameter name: {paramName}{Environment.NewLine}" +
                  $"{message}")
        {
        }
    
        #endregion
    
        public static void ThrowOnNullOrEmpty(string paramName, string paramValue)
        {
            if(string.IsNullOrEmpty(paramValue))
                throw new ArgumentNullOrEmptyException(paramName);
        }
        public static void ThrowOnNullOrEmpty(string paramValue)
        {
            if (string.IsNullOrEmpty(paramValue))
                throw new ArgumentNullOrEmptyException();
        }
    
    
        public static void ThrowOnNullOrEmpty(string paramName, object paramValue)
        {
            //ThrowOnNullOrEmpty another object that could be 'empty' 
    
            throw new NotImplementedException();
        }
    }
    
    
    
            private static void AFunctionWithAstringParameter(string inputString)
        {
            if(string.IsNullOrEmpty(inputString)) throw new ArgumentNullOrEmptyException(nameof(inputString));
    
            //Or ..
            ArgumentNullOrEmptyException.ThrowOnNullOrEmpty(nameof(inputString), inputString);
        }
    

        4
  •  0
  •   Hutch    8 年前

    另一种可能性是 ArgumentOutOfRange 例外情况:

    当参数的值超出所调用方法定义的允许值范围时引发的异常。