Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct exception for an empty\null string passed to a constructor [duplicate]

Tags:

I have a class:

class Foo
{
    public Foo(string bar)
    {
        if (string.IsNullOrEmpty(bar))
            throw new Exception("bar must not be null or empty.");
    }
}

What is the most correct exception type to throw?

Viable candidates are:

  1. ArgumentNullException
  2. ArgumentException
  3. InvalidOperationException
  4. TypeInitializationException (not this as per dasblinkenlight below)

My instinct is to go with InvalidOperationException, as the caller is attempting to construct the object in a illegal state, though ArgumentException has merits as well.

I wish there was a StringNullOrEmptyException, wouldn't that be great?

Edit Thanks for the suggested question, it is similar, but I was asking specifically about it happening in the constructor and whether that would change the recommendation at all.

like image 321
BanksySan Avatar asked Nov 22 '13 21:11

BanksySan


People also ask

How do you throw an exception if a string is null?

In Java, the java. lang. NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.

What is a null reference exception?

A NullReferenceException happens when you try to access a reference variable that isn't referencing any object. If a reference variable isn't referencing an object, then it'll be treated as null .

How do you handle empty string exception in Java?

The isEmpty() method checks whether a string is empty or not. This method returns true if the string is empty (length() is 0), and false if not.


2 Answers

I suppose the most correct implementation would be this:

if (bar == null) { throw new ArgumentNullException (...); }
else if (bar.Trim() == "") { throw new ArgumentException (...); }

but we might be straining a gnat and swallowing a camel. It's probably not terribly important.

On the other hand, you could build the StringNullOrEmptyException class.

like image 163
Mike Perrenoud Avatar answered Oct 20 '22 06:10

Mike Perrenoud


One very common way of handling situations like this is to throw two different exceptions - one for the null, and another one for the invalid non-null string:

if (bar == null) {
    throw new ArgumentNullException("bar");
}
if (string.IsNullOrWhiteSpace(bar)) {
    throw new ArgumentException("bar");
}

Since you mentioned other exceptions, here is what they signify:

  1. ArgumentNullException - Indicates that the argument in question is null
  2. ArgumentException - Indicates that the argument in question is not null, but is otherwise invalid.
  3. InvalidOperationException - Indicates that the operation cannot be performed in the current state of the object.
  4. TypeInitializationException - Indicates that the type (not an instance of the type, but the type itself) cannot be initialized.

The first three exceptions from this list always indicate a programming problem on the side of the caller, i.e. callers receiving them know that they must fix their code, because they are calling your API incorrectly.

The last exception indicates a programming problem on your side, i.e. the callers receiving this error know that they must call you to fix your error, or reconfigure the way in which they installed your library.

like image 39
Sergey Kalinichenko Avatar answered Oct 20 '22 06:10

Sergey Kalinichenko