954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

IllegalStateException?

I've looked and looked for some good material on this exception, but I can't find anything helpful. I just don't understand it. It's suppose to be caught or thrown whenever a method is called when not suppose to be...But won't other exceptions be caught or thrown in it's place? If you have any comments or anything to say, please post!

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

but then u never catch this expection in the first place. if you want to catch all expections can try using this

try
{}
catch(Exception e)
{
}
starsunited
Light Poster
44 posts since Dec 2004
Reputation Points: 11
Solved Threads: 1
 

I know that, but I just don't undestand this exception. What is it for? I mean, when is a method called, but not suppose to be called?

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

Think of calling a method to do a database query on a database that is closed.
Or calling a read method on a stream that's past the end of the data.
Or calling write methods on a readonly file.

Note that these won't throw IllegalStateException in Java but they could (they throw something more specific).

In general it's there to indicate you're trying to do something with an object that at that moment isn't possible to do with it.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 
Note that these won't throw IllegalStateException in Java but they could (they throw something more specific).

This is what I was looking for. I was thinking this was the case,but wasn't sure. So I guess this means that this exception would have to be pretty high on the exception hierarchy?

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

It is. But remember File et.al. typically throw IOExceptions which don't derive from IllegalStateException.

IllegalStateException is a RuntimeException, which is why you'll find few cases of it being explicitly caught (usually these are used to indicate programming errors rather than runtime problems, despite what their name suggests) while IOException is a regular Exception which requires catching at some point.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

So would it be called bad practice to throw one of these? I mean, couldn't it be almot considered as doing a catch-all-throw clause like throws Exception?

Have you ever seen this exception used before?

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

I've seen it used in Google DDMLib, when init() was called twice (since the library was already initialized, it was not in a state to be initialized again).
It was an extremely annoying use of the exception, because 1) there is no indication in the init() method signature that it would happen, and 2) it is an unchecked exception so it is supposed to indicate that the program is in an unrecoverable state and should shut down.

This exception is fine to use, but be aware that when you use it, you are basically implying that the program should stop immediately (or that particular subsystem should stop immediately).

GoogleException
Newbie Poster
1 post since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You