C
Chris Smith
Tony said:The illegal state is that the developer foresaw that the default case
would never occur. If it does occur, the application is in an illegal
state.
"... Java application is not in an appropriate state for the requested
operation."
Interesting perspective, but I still think I disagree. The "illegal
state" means the state is incorrect for performing this operation right
now, *not* that a bug in the method's implementation caused illegal
state of its own local variables. There's no reason to believe that
this problem occurred because of the state of the application; and in
fact it's not even very likely. The code in question never accessed any
application state at all, but only depended on its arguments. If I had
to guess one way or the other between the two API-enforcing exceptions,
I'd guess that IllegalArgumentException is the more likely to be
accurate. (Fortunately, no one has to guess. A more generic exception
is quite suitable.)
To verify the usage of IllegalStateException, check places it's used in
standard APIs. It's thrown, for example, from Servlet.addHeader once
the response is committed; or if a security MAC algorithm is used prior
to initializing it.
Basically, it comes down to this, if I write code and get back an
IllegalStateException, I'm going to start looking for why I shouldn't
have called that method right now. If I then find out that it's
actually an implementation bug in the library code, I will have been
deceived by the odd choice of exception.
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation