I'm working on making a codebase complaint with a list of PMD rules. These include that you're not allowed to throw or catch NullPointerException
s. I found this method which requires 2 instance variables to be non-null in order to run. It throws a NullPointerException
otherwise:
public String build() {
if (apiKey == null || url == null) {
throw new NullPointerException(
"Cannot build: API key and URL must be set");
}
What should it be throwing instead? it seems to me that NullPointerException
would be the bast call. The only other thing that comes to mind is IllegalArgumentException
, but these aren't really arguments, so I don't think it really fits.
IllegalStateException
seems entirely appropriate here:
Signals that a method has been invoked at an illegal or inappropriate time.
That describes the situation reasonably clearly, doesn't it?
See more on this question at Stackoverflow