1

It's confusing: We all know the words from Doug Lea: "Null sucks". He is right.

Now I there are some approaches to throw exceptions by using googles guava. But what would be the right way now?

1) throwing an NullPointerException

private void foo(Object arg) {
  Preconditions.checkNotNull(arg, "arg 'null' not accepted");
}

2) or an IllegalArgumentException

private void foo(Object arg) {
  Preconditions.checkArgument(arg!=null, "arg 'null' not accepted");
}

3) or a VerifyException

private void foo(Object arg) {
  Verify.verify(arg!=null, "arg 'null' not accepted");
}

I mean, isn't the first approach obsolete cause we don't have a NullPointer access at this time? And why this new third approach.

Could someone lift the fog?

7
  • Depending on the analysis, from time to time, null is a valid response. If it is not, try and figure out whether it was an error in the business logic, or the technical background that caused the null. Commented Feb 18, 2015 at 8:59
  • 1
    Update to Java8 and return Optional<T> Commented Feb 18, 2015 at 9:00
  • Check out the docs for guidance on the same. They can be used interchangeably for most situations. Commented Feb 18, 2015 at 9:08
  • I agree that Optional is good approach, but sometimes you need to throw excpetion (Transaction rollback, etc.). As you checking argument, I will go with IllegalArgumentException. You can have many cases when argument is illegal, NULL is one of them. (i.e. NULL, below zero, is even, etc. ) Commented Feb 18, 2015 at 9:22
  • 2
    possible duplicate of IllegalArgumentException or NullPointerException for a null parameter? Commented Feb 18, 2015 at 9:28

3 Answers 3

4

I agree with Aaron. My two cents is that since in Java 7 Objects.requireNonNull was added and it throws a NullPointerException, that is the way I go. To me it seems like the language developers have weighed in on this issue by doing so.

Sign up to request clarification or add additional context in comments.

Comments

3

There is no right way. Some people argue NPE since you passed a null pointer. Plus you can directly use Preconditions.checkNotNull() without any additional checks in code that you have to write (!=null is another 6-10 key presses, after all, plus people have to read more code to understand what you're doing).

Preconditions.checkArgument() makes more sense from an API point of view since the method doesn't want null arguments. Plus you have additional code to write and to read when you're debugging.

Verifify seems too weak for this for me, after reading the documentation.

Comments

1

"Effective Java" seems quite adamantly in favor of throwing a NullPointerException IllegalArgumentException or NullPointerException for a null parameter?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.