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

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

    Well, it's not an argument, if you're referencing a property of a class. So, you shouldn't use ArgumentException or ArgumentNullException.

    NullReferenceException would happen if you just leave things alone, so I assume that's not what you're looking for.

    So, using ApplicationExeption or InvalidOperationException would probably be your best bet, making sure to give a meaningful string to describe the error.

提交回复
热议问题