As the title says, I am wondering what the best practice is regarding the throwing of NullPointerExceptions. Specifically, if I have an external library function that can return
I'd say that you should never explicitly create a NullPointerException
and instead use an exception type that more clearly describes the situation. In your case, I'd say IllegalStateException
would fit the situation "wrong setup of the system, and pretty unrecoverable from at runtime". Or you could create your own ComponentMissingException
. In cases where a required method parameter is null
, IllegalArgumentException
is typically used.