Far als Best Practices sind besorgt, was besser ist:ArgumentException oder ArgumentNullException für String-Parameter?
public void SomeMethod(string str)
{
if(string.IsNullOrEmpty(str))
{
throw new ArgumentException("str cannot be null or empty.");
}
// do other stuff
}
oder
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
}
Die zweite Version scheint präziser, aber auch umständlicher als die erste. Ich gehe normalerweise mit # 1, aber ich dachte, ich würde prüfen, ob es ein Argument für # 2 gibt.
[Mögliches Duplikat] (http://stackoverflow.com/questions/1355957/should-i-throw-argumentnullexception-if-a-string-is-blank)? Dort schlagen sie auch eine andere Option vor: eine benutzerdefinierte 'StringNullOrEmptyException'. –