ArgumentException or ArgumentNullException for string parameters?
Posted
by Anna Lear
on Stack Overflow
See other posts from Stack Overflow
or by Anna Lear
Published on 2010-03-19T20:54:26Z
Indexed on
2010/03/19
21:01 UTC
Read the original article
Hit count: 239
Far as best practices are concerned, which is better:
public void SomeMethod(string str)
{
if(string.IsNullOrEmpty(str))
{
throw new ArgumentException("str cannot be null or empty.");
}
// do other stuff
}
or
public void SomeMethod(string str)
{
if(str == null)
{
throw new ArgumentNullException("str");
}
if(str == string.Empty)
{
throw new ArgumentException("str cannot be empty.");
}
// do other stuff
}
The second version seems more precise, but also more cumbersome than the first. I usually go with #1, but figured I'd check if there's an argument to be made for #2.
© Stack Overflow or respective owner