So, I am working on creating a class in which if certain arguments for certain methods are equal to zero, either the method (or the object as a whole) will not work.
I know that he will throw a NullPointerException after receiving a null object and try to use it, but I want the programmer to try to call the method to understand that the error is not in my code. I just want to make sure that the resulting exception would be very clear (without having to look into my source).
I saw some 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"); ... }
Thus, the programmer understands that he violated the contract implied by javadoc (regardless of whether this is explicitly indicated).
Is this a good practice or even a sensible thing?
Quick Editing / Sidebar:
What is better to say in the exception message?
Better to say that โwent wrongโ:
someObject is null
Or is it better to say that something โwent wrongโ and generally implies a reason (and ultimately a solution):
someObject cannot be null
Or is there a better alternative?
source share