944,047 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 9580
  • Java RSS
Apr 19th, 2005
0

IllegalStateException?

Expand Post »
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!
Similar Threads
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Apr 20th, 2005
0

Re: IllegalStateException?

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

Java Syntax (Toggle Plain Text)
  1. try
  2. {}
  3. catch(Exception e)
  4. {
  5. }
Reputation Points: 11
Solved Threads: 1
Light Poster
starsunited is offline Offline
44 posts
since Dec 2004
Apr 20th, 2005
0

Re: IllegalStateException?

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?
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Apr 20th, 2005
0

Re: IllegalStateException?

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Apr 20th, 2005
0

Re: IllegalStateException?

Quote originally posted by jwenting ...
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?
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Apr 20th, 2005
0

Re: IllegalStateException?

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Apr 20th, 2005
0

Re: IllegalStateException?

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?
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Sep 9th, 2011
0
Re: IllegalStateException?
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).
Reputation Points: 10
Solved Threads: 0
Newbie Poster
GoogleException is offline Offline
1 posts
since Sep 2011

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Reading object from a file
Next Thread in Java Forum Timeline: Class not found error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC