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.