ArgumentException or ArgumentNullException for string parameters?

前端 未结 4 589
南旧
南旧 2020-12-14 06:52

Far as best practices are concerned, which is better:

public void SomeMethod(string str) 
{
    if(string.IsNullOrEmpty(str))
    {
        throw new Argumen         


        
4条回答
  •  遥遥无期
    2020-12-14 07:21

    Throwing a null reference on an empty string can be highly confusing - the string's default value is null and would indicate a not initialized string whereas and empty string may constitute other problems.

    I like the idea of Jon Skeet however i like to explicitly use throw, and not have a separate function.

    Instead you could throw the following class:

     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);
        }
    

    Note that my derived type only calls the base exception class instead of overriding the message property. This is because the 'additional info' of the visual studio debugger lists the internal message - not the overridden type. This means that this is the only way to display the message nicely whilst debugging.

提交回复
热议问题