Try Catch or If statement?

前端 未结 10 1864
北恋
北恋 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:28

    In general, try-catch blocks are great because they will break (move to the catch statement) whenever the exception occurs. If-else blocks rely on you predicting when the error will happen.

    Also, catch blocks won't stop your code from halting when an error is hit.

提交回复
热议问题