Take this method
/**
* @return List of group IDs the person belongs to
*
*/
public List<String> getGroups() {
if (this.getId().equals("")) return null;
}
I would like to throw exception instead returning null, what’s the exception to throw when an important parameter/dependency has not been set?
I’d use
IllegalArgumentExceptionif the parameter/argument is controlled from outside, orIllegalStateExceptionif the method is just called at a wrong moment (state). In your specific case I think it’s the latter. A (dubious) alternative isNullPointerException.This should however be explicitly documented in the
@throwsso that the user understands the reason.