What exception type to use when a property cannot be null?

前端 未结 9 1966
被撕碎了的回忆
被撕碎了的回忆 2020-12-30 18:41

In my application I need to throw an exception if a property of a specific class is null or empty (in case it\'s a string). I\'m not sure what is the best exception to use

9条回答
  •  灰色年华
    2020-12-30 19:40

    If it can't be null or empty, have your setter not allow null or empty values, or throw an ArgumentException if that is the case.

    Also, require that the property be set in the constructor.

    This way you force a valid value, rather than coming back later and saying that that you can't determine account balance as the account isn't set.

    But, I would agree with bduke's response.

提交回复
热议问题