Throwing ArgumentNullException

前端 未结 12 2210
名媛妹妹
名媛妹妹 2020-12-08 02:10

Suppose I have a method that takes an object of some kind as an argument. Now say that if this method is passed a null argument, it\'s a fatal error and an exception shoul

12条回答
  •  一生所求
    2020-12-08 02:15

    I'll probably be downvoted for this, but I think completely different.

    What about following a good practice called "never pass null" and remove the ugly exception checking?

    If the parameter is an object, DO NOT PASS NULL. Also, DO NOT RETURN NULL. You can even use the Null object pattern to help with that.

    If it's optional, use default values (if your language supports them) or create an overload.

    Much cleaner than ugly exceptions.

提交回复
热议问题