method to return a String depending on various cases

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
 
T

Tony Morris

Chris Smith said:
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.)

Good point.
Perhaps a java.lang.RuntimeException (or your own subclass of).

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 
T

Timo Kinnunen

Yes, that makes sense, or to use more normal syntax:

default: assert false;

Unfortunately, that results in "missing return statement" compile-time
error, because assertions can be turned off.
 
C

Chris Smith

Timo said:
Unfortunately, that results in "missing return statement" compile-time
error, because assertions can be turned off.

Oops, yeah. You're right; forget I said that.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top