In Java, if null pointers rarely occur, it’s best to use catch instead of if

Clean up some loose in my Android application. I found a null pointer exception in the developer console. This has never happened. I guess this is a rare competitive condition

For those who don't know: Android allows users to report crashes (i.e. uncapped exceptions) to developers

When I came across the infamous if (...! = null), I had started typing: there were only three reports So it rarely happens

So I want to know: in this case, performance wise: would it be better to catch null pointer exceptions?

Considering that if. Is evaluated every time

Solution

Don't sound like a fanatic or narrow-minded person, but I firmly believe that NPE should never be allowed to happen first! In my opinion, capturing NPE is a very bad practice This means that you don't fully understand how the code works

The first thing you should always do before using some input is to check for null

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>