让我们看看这两个例子。
第一的:
try {
    execute(testObj);
} catch(Exception e) {
    //do somethingwith that
}
public void execute(TestObj testObj) throws Exception {
    if (testObj == null){
        throw new Exception("No such object");
    }
    //do something with object
}
第二:
if (testObj != null){
    execute(testObj);
} else {
    //handle this differently
}
public void execute(TestObj testObj) {
    //do something with object
}
如果我们需要检查“is null”或其他任何内容,这不是现在的重点。我想知道哪种做法总体上更好——“检查,然后执行”或“执行,如果发生则处理异常”?