This is a silly question - but it bugs me.
From the existing (library) Java exceptions, which should I throw in the following.
I have a method that is used in the wrong scenario (it's basic assumption doesn't hold).
This method has no arguments - so I tend to skip the IllegalArgumentException.
As an example - consider a BinaryNode class having just two left/right child nodes.
For brevity it's nice to have a removeOnlyChild() method, which applies only if this node actually has just one child (not 0 or 2).
Obviously if someone calls n.removeOnlyChild() on a node n that has 2 children, an exception should be thrown.
Out of the standard Java exceptions - which do you think it should be, and why?
I'm actually going over the list every-once-in-a-while, when this pops-up, and just go with IllegalStateException or with InternalError.
InternalErroror any other subclass ofErrorunless you have a good reason to do so. By design, subclasses ofErrorwon't be caught by... } catch (Exception ex) { ...