Try Catch or If statement?

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

    In my experience using if is better but only if you actually expect a null reference pointer. Without any bit of code or context its difficult to say when one option is better than the other.

    There's also a matter of optimization - code in try-catch blocks won't be optimized.

提交回复
热议问题