If I have a function below with two options
private MyObject findBlank() {
for (int i = 0; i < pieces.length; i++) {
if(pieces[i].isBlank()){
return pieces[i];
}
}
return null;
}
private MyObject findBlank() {
for (int i = 0; i < pieces.length; i++) {
if(pieces[i].isBlank()){
return pieces[i];
}
}
throw new NoSuchFieldError("No blank piece found!");
}
From this method, I know that it should always return an object , one of the “pieces” is always equal isBlank() == true, the return null at the end is just to please the compiler. Since this is the case, and my code will not work anyway if it returns null, is it right to do this to throw an exception?
My parameters:
- return null and the application will get a NullPointerException in some edge case
- returns null and ends using the method with (myObject! = null) checks
- introduce an exception that will explode at runtime
, , ? .. , . "", , ( ). , null, ?
, , , ?