等待空指针异常发生是否更好?
public void doSomething(String str) {
Double val = Double.parseDouble(str); // Null pointer exception thrown here
// Other code
}
还是最好每次都检查一次,越早越好?
public void doSomething(String str) {
if (str == null)
throw new NullPointerException(); // Null pointer exception thrown here
Double val = Double.parseDouble(str); // Other code
}
最佳答案
我建议使用断言子句。我认为他的回答最能回答您的问题
关于java - 在每个方法的开头检查 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33801756/