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

前端 未结 9 1968
被撕碎了的回忆
被撕碎了的回忆 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:21

    If the problem is that a member of an argument, and not the argument itself, is null then I think the best choice is the more generic ArgumentException. ArgumentNullException does not work here because the argument is in fact not null. Instead you need the more generic "something is wrong with your argument" exception type.

    A detailed message for the constructor would be very appropriate here

提交回复
热议问题