views:

105

answers:

5

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?

+9  A: 

I'd use IllegalArgumentException if the parameter/argument is controlled from outside, or IllegalStateException if the method is just called at a wrong moment (state). In your specific case I think it's the latter. A (dubious) alternative is NullPointerException.

This should however be explicitly documented in the @throws so that the user understands the reason.

BalusC
NPE and IAE just don't make any sense in the context of the question. You're giving bad advice.
Stefan Kendall
The context of the question is indeed limited. As far as I can see, the ISE is the best choice. I also explicitly stated that, so I don't see how that is bad :)
BalusC
Is there a magic how can I update the comment section in Eclipse to automatically regen missing @params and add @throws section? Maybe a shortcut?
Pentium10
Sorry, don't know that. It will be automatically added for non-runtime-exceptions (when you generate/update the `throws` clause), but not for runtime-exceptions.
BalusC
In Eclipse, you can use 'Alt-Shift-J' when the cursor is at the beginning of your method declaration to generate the method javadoc comment. That will generate a new javadoc block for you, though, it won't add on to what's there, so not sure it's all that helpful for existing documented methods.
elduff
+2  A: 

How about IllegalStateException?

Stefan Kendall
A: 

I would use an IllegalStateException because the id is state of the owner. If the id would have passed as parameter, an IllegalArgumentException would be right.

Arne Burmeister
Why is my answer downvoted? It nearly the same as the accepted one, expect the ugly idea of the NullPointerException!
Arne Burmeister
A: 

I would create my own Exception type by extending Exception. That way calling functions can catch that particular Exception and handle it gracefully as appropriate. Note, you can do the same thing with just about anything that Extends Exception, but I prefer to create my own Exception classes so I can be very robust in my exception handling. This is, of course, up to you though.

Jay
Why reinvent the wheel? IllegalArgumentException is made exactly for an illegal argument.
Steve Kuo
It depends on how many different types of illegal arguments you want to trap for. I like extending Excpetion, or in this case IllegalArgumentException just to have a specific Exception class to deal with different types of conditions I want to deal with. It makes Exception processing easier and more robust, IMHO.-Jay
Jay
A: 

If its not possible to ensure that the id is always set (by requiring it in the constructor for example, where you could check that a valid id has been passed) then I think the other suggestions to throw IllegalStateException are correct. But it would be better to try and ensure that your object can't get into this state in the first place if at all possible

Sam Holder
please add a comment if you downvote explaining why.
Sam Holder