Which exception should be thrown from a set of properties?

In .NET, what type of exception should be thrown if someone passes in an invalid value for part of a set { }

property?

Example:

public string Provider
{
    get { return _provider; }
    set
    {
        if (String.IsNullOrEmpty(value)) throw new Exception("Provider cannot be null or empty."); //what type of exception should be thrown here instead?
        _provider = value;
    }
}

      


Note:

I am asking this question as it relates to .NET, but it can apply to many other languages. So if you have a good answer that relates to something other than the .NET Framework, post it!

+1


source to share


4 answers


ArgumentException , ArgumentOutOfRangeException, ArgumentNullException, or similar.



+7


source


Generally speaking, I would disagree with John B. as to when to throw an exception - if triggered by a call to set (), it will display the context when an invalid value was set. On the other hand, if you just set it to some dummy value and throw an exception elsewhere, then depending on the approach you take, the client will either see the dummy value without indicating that it is not the value that is actually set, or they will receive an exception that does not give them an idea of ​​who is responsible for the wrong condition or how to fix it.

Now in some situations this method might make sense - if for some unavoidable reason the property is extremely volatile and you decide it is okay for illegal properties to be set temporarily, but (based on some external synchronization) the client is not allowed to fetch the property until it becomes a legal, stable value. This is not great either, but I'm sure you can be in a situation where you cannot help in this situation.



And since you were asking about languages ​​other than .NET, in Java I would use IllegalArgumentException in general - perhaps the old NullPointerException if the argument is null.

+1


source


I agree with Andrzej. The exception must be thrown in "set" because the property should NEVER be set to an invalid value and you need to "grab the context when an invalid value was set"

I am using one of the ArgumentException classes because "under the hood" "set" is a call to an automatically generated method named "CLASSNAME'.set_'PROPERTYNAME" (value)

for example

class MyClass
{
  string name;

  public string Name {
    get {
      return name;
    }
    set {
      if (value==null) {
        throw new ArgumentNullException("value", "The value of the property Name cannot be set to null.");
      }
      name = value;
    }
  }

}

      

Internally, this creates two methods

public string get_Name()

      

and

public void set_Name(string value)

      

If you are showing a stack trace this will be an exception from.

This is why I always include the text "Property Value Name ..." in the message in the message so that the property user can see where the exception was thrown, because there will be no method named set_SOMEPROPERTY (SOMETYPE value) visible to the user of the class library.

(also, this is what you intend to do according to "NET Framework 4 -Design Directives for Exceptions, Catching and Throwing Standard Exception Types ( Here ) About half a page under" ArgumentException, ArgumentNullException and ArgumentOutOfRangeException "it says" Use value for parameter name implicit property value ")

+1


source


I would not throw an exception in this case. If the property is a string, then I can set it to "" or null. An exception should be thrown when I try to use this property (for example, in the Connect () function), with the message that the function does not work because the property was invalid.

0


source







All Articles