So, I’m working on designing a class wherein if certain arguments to certain methods are null, either the method (or the object as a whole) won’t work.
I know that it’ll throw a NullPointerException once it receives the null object and attempts to use it, but I want the programmer trying to call the method to understand that the bug is not in my code. I just want to ensure that the resulting exception thrown would be very clear (without the need to look into my source).
I’ve seen a few examples of what I described, where they throw an IllegalArgumentException when the parameter is null.
Here’s the difference, imagine that someObject will somehow be vital to the method:
public void doSomething(SomeClass someObject) {
if (someObject == null) throw new IllegalArgumentException("someObject is null");
...
}
This way, the programmer understands that he or she has broken the contract implied by the javadoc (whether or not it is explicitly stated).
Is that good practice, or even a reasonable thing to do?
Quick Edit/Side-bar:
What would be best to say in the exception message?
Is it better to state what "went wrong":
someObject is null
Or is it better to state that something "went wrong" and generally imply the cause (and ultimately the solution):
someObject cannot be null
Or is there a better alternative?
Use an
IllegalArgumentExceptioninstead of allowing theNullPointerExceptionso that you can discover the error as early as possible. You could perform the same check and throw aNullPointerExceptionyourself as well, but that’s a matter of semantics.By throwing the error immediately, it helps you or some other developer catch the mistake before anything else happens. If your method doesn’t use the argument immediately (for instance, in a setter), then the problem could appear to be the result of some completely different operation.
If you’re not familiar with it, a fantastic book called The Pragmatic Programmer has a tip that I’m trying to emphasize here:
The main idea is to fail before anything bad can happen, rather than allow problems to crop up at unexpected times.
This would also be a good place to use an assertion to enforce your precondition. Make sure you document that the argument cannot be null, and make your method look like this:
If the assertion fails, an error will be thrown. While this could be turned off with compiler options, it’s a good practice during development.