Should I throw an ArgumentNullException if the string is empty?

I am working on a method that does something given by a string parameter. A valid value for the string parameter is a value 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. My question is, is it possible to throw an ArgumentNullException even if the string is equal to string.Empty? Because technically this is not empty. If you think this should not raise an ArgumentNullException, what exception should be thrown?

+49
Aug 31 '09 at 5:42
source share
6 answers

An ArgumentException should be thrown for the case of String.Empty . This indicates a problem other than zero. To avoid a NullReferenceException , I first check for null, then I crop and check for an empty case to prevent any spaces from being skipped.

 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 } 

In .NET 4.0, you can use the String.IsNullOrWhiteSpace method to perform these checks at a time. By doing so, you are refusing the ability to specify a granular type of exception, so I would choose ArgumentException and update the message accordingly.

+34
Aug 31 '09 at 5:44
source share

You should throw an ArgumentException if an empty string is not accepted input for your method. This can be very confusing for clients if you selected ArgumentNullException before they provided a null argument.

This is just another case. You may also have methods that do not accept null input values, but that accept empty strings. It is important to be consistent throughout your application.

+5
Aug 31 '09 at 5:46
source share

Given all of the above (Joe / Ahmad Mageed), I would make an exception for this case.

 class ArgumentNullOrEmptyException : ArgumentNullException 
+4
Aug 31 '09 at 6:07
source share

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

Therefore, I think it is wise to do the same in your code, unless there is some real value to distinguish between the two cases.

Please note that these and other exceptions derived from an ArgumentException typically indicate a programming error and, therefore, you must provide the information necessary to help the developer diagnose the problem. Personally, I find it unlikely that the developer will find this 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) { ... } 
+1
Aug 31 '09 at 5:57
source share

It really depends on the circumstances.

The question boils down to whether this is really a mistake? By this I mean that you always expect value? If you do this, then probably your best bet here is creating your own Exception , perhaps like this:

 class StringEmptyOrNullException : Exception { } 

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

If it is, however, not "exceptional" occurring in your program, if it is probably the best idea to return null from the method and process it from there. Just remember, Exception is for exceptional conditions.

Hope this helps,

Kyle

0
Aug 31 '09 at 6:15
source share

Why not 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
11 Oct 2018-10-10
source share



All Articles