java version "1.7.0_75"
Hello,
Just wondering what is the prefered best practice comparing the 2 functions below.
The first one throws a NullPointerException that should be captured in the calling function. The second one just returns false if there has been a null pointer exception.
Throw an exception:
public void disconnect() throws NullPointerException {
if(mClientConnection == null) {
throw new NullPointerException("mClientConnection has an invalid reference");
}
if(mClientConnection.isConnected()) {
mClientConnection.disconnect();
}
mClientConnection = null;
}
Just return true or false:
public boolean disconnect() {
if(mClientConnection == null) {
log.log(Level.SEVERE, "Cannot disconnect as mClientConnection is null");
return false;
}
if(mClientConnection.isConnected()) {
mClientConnection.disconnect();
}
mClientConnection = null;
return true;
}
Normally in the past I have always gone with the second one by just return true or false. But so now I am just looking for alternative solutions.
Many thanks for any suggestions,