I\'m wondering about the cost of using a try/exception to handle nulls compared to using an if statement to check for nulls first.
To provide more information. There
If passing null
argument is an exceptional case, then I'd throw a NullPointerException
.
public Result calculate(Input input) {
if (input == null) throw new NullPointerException("input");
// ...
}
If passing null
is an allowed case, then I'd skip the calculation and eventually return null
. But that makes in my opinion less sense. Passing null
in first instance would seem a bug in the calling code.
Whatever way you choose, it should be properly documented in the Javadoc to avoid surprises for the API user.