What exception to throw from a property setter?

后端 未结 7 2104
醉话见心
醉话见心 2020-12-08 02:24

I have a string property that has a maximum length requirement because the data is linked to a database. What exception should I throw if the caller tries to set a string ex

7条回答
  •  一生所求
    2020-12-08 03:08

    Have a look through mscorlib.dll with Reflector, in a similar situation such as System.String.StringBuilder.Capacity Microsoft use ArgumentOutOfRangeException() similar to:

    public int PropertyA
    {
        get
        {
            return //etc...
        }
        set
        {
            if (condition == true)
            {
                throw new ArgumentOutOfRangeException("value", "/* etc... */");
            }
            // ... etc
        }
    }
    

提交回复
热议问题