Viễn như thông lệ tốt nhất là có liên quan, mà là tốt hơn:ArgumentException hoặc ArgumentNullException cho tham số chuỗi?
public void SomeMethod(string str)
{
if(string.IsNullOrEmpty(str))
{
throw new ArgumentException("str cannot be null or empty.");
}
// do other stuff
}
hoặc
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
}
Phiên bản thứ hai có vẻ chính xác hơn, mà còn nhiều rườm rà hơn trước. Tôi thường đi với # 1, nhưng tôi đã kiểm tra nếu có một đối số được thực hiện cho # 2.
[Có thể trùng lặp] (http://stackoverflow.com/questions/1355957/should-i-throw-argumentnullexception-if-a-string-is-blank)? Ở đó, họ cũng đề xuất một tùy chọn khác: tùy chỉnh 'StringNullOrEmptyException'. –