Try Catch or If statement?

前端 未结 10 1856
北恋
北恋 2020-12-15 17:47

if you think there is a possibility of getting a null pointer exception, should you use an if statement to make sure the variable is not null, or should you just catch the e

10条回答
  •  借酒劲吻你
    2020-12-15 18:33

    Well. Exceptions are just that. Exceptions. They are thrown when something unforseen has happened and should not be part of the normal program flow.

    And that's what is happening here. You expected the argument to be specified when it's not. That is unexpected and you should therefore throw your own exception informing the user of that. If you want to get bonus points you can also include the reason to WHY the argument must be specified (if it's not obvious).

    I've written a series of posts about exceptions: http://blog.gauffin.org/2013/04/what-is-exceptions/

提交回复
热议问题