How do I return with errors in a method?
Let's assume there is a method:
MyClass myMethod(MyClass input){
//body of method
}
Suppose I don't know where exactly this method can be used. If the input is null, then how can I decide if I should throw an exception or just return a null value?
I have the same question for a method like
void myMethod(MyClass input){
//body of method
}
If the input is null, should I just return without doing anything, or throw an exception, or say System.error ()?
I have used all of this for my projects. This all works great as most of my classes have very few public methods.
source to share
If the input is null, then how do I decide whether I should exclude or just return a null value?
ask yourself is there a null
valid input? if so, don't throw an exception and don't handle it logically. if no, throw exception.
What really matters here: Let's say you store some values in an object, and some of them are optional. that is, the user may or may not provide values there. in this case null
will only be valid for those fields, but not valid for mandatory fields null
.
Apply similar thinking to your problem and you can come to a conclusion.
Another example: Let's say you open a file that this method is entered into filePath
. if the specified path is null, it is not valid and an exception should be thrown.
source to share
If the method is not expected to be called with a null argument, it is fine to throw a NullPointerException (NPE short-circuited) as this is a precondition that input! = Null and the caller should have checked it beforehand.
/** My method.
* @param input the input, must not be null.
*/
void myMethod(MyClass input){
if (input==null) throw new NullPointerException();
//...
}
A common idiom for throwing NPEs without increasing your branch count of your program is:
void myMethod(MyClass input){
input.getClass(); //NPE if input is null
//...
}
In some cases, the above check is implicit in the code:
void printLowercase(String input){
System.out.println(input.toLowerCase());
}
Avoid using a method that fails because it makes it difficult for the caller to call to see if the method has fired. Instead, we return a boolean value.
boolean myMethod(MyClass input){
if (input==null) {
//you may log that the input was null
return false;
}
//...
return true;
}
source to share
Answer
Tell me don't ask
The idea is to delegate error handling to an object that can decide whether to return some value or throw an exception.
Once you have control over the method signature, you can simply do
MyClass myMethod(MyClass input, Expectation whenInputIsNull){
if(input==null) {
// might also throw
return whenInputIsNull.handle();
}
//body of method
}
If you have no control over the method signature, you can put Expectation
as a member of the owner class myMethod
.
It might be very enjoyable to read:
foo.myMethod(input, Expect.nullReturned());
foo.myMethod(input, Expect.throwIllegalArgumentException());
I find it very convenient when this is used with repositories:
fooRepository.findByBar(bar, Expect.atLeast(5));
source to share