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:
ArgumentNullException
ArgumentException
InvalidOperationException
TypeInitializationException
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.
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.
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 .
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.
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.
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:
ArgumentNullException
- Indicates that the argument in question is null
ArgumentException
- Indicates that the argument in question is not null, but is otherwise invalid.InvalidOperationException
- Indicates that the operation cannot be performed in the current state of the object.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With