This is a stupid question, but it bothers me. Of the existing (library) Java exceptions, which I should use in the following. I have a method that is used in the wrong scenario (this basic assumption is not fulfilled).
This method has no arguments - so I tend to skip IllegalArgumentException.
As an example, consider a class BinaryNodethat has only two child nodes on the left and right.
For brevity, it’s nice to have a method removeOnlyChild()that applies only if this node has only one child (not 0 or 2).
Obviously, if someone calls n.removeOnlyChild()on a node nthat has 2 children, an exception should be thrown.
Of the standard Java exceptions - which, in your opinion, should be and why?
I really go through the list every time it pops up and just walk with IllegalStateExceptionor InternalError.
source
share