• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Illegal State Exception

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers,
I am new to this forum. I have a doubt about K &B Self test Chapter 5 Q16. Below is the question -

Which are most typically thrown by an API developer or an application developer as opposed to being thrown by the JVM? (Choose all that apply.)
A. ClassCastException
B. IllegalStateException
C. NumberFormatException
D. IllegalArgumentException
E. ExceptionInInitializerError

Answer is - B,C & D.

I agree with C & D. But I am not able to understand how B is also correct answer. I have never come across any code where we throw IllegalStateException as part of our programming. Hope to get ot clarified from the Ranchers.

Thanks,
Sushant
 
Master Rancher
Posts: 4908
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Sushant]: I have never come across any code where we throw IllegalStateException as part of our programming.

The intent of the question appears to be: which of these exceptions are thrown by API or application developers more often than they are thrown by the JVM? So to answer this, you need to know something about:

How often is this exception thrown by API or application developers?

How often is this exception thrown by the JVM?

Now if you've never encountered this exception ever in any context, you probably need to study more before you take the exam - particularly, study threads and the wait()/notify() methods.

To answer this question, I think about the fact that I can't think of any circumstance where the JVM throws this exception. But I can think of several circumstances where an API developer (from Sun) has chosen to throw this exception. Two examples would be in the API for Object's wait() and notify() methods, which are part of the exam objectives, as I recall. So I would say that it's more likely that this exception will be thrown by an API developer, as opposed to the JVM. Even if event A is rare, if event B never occurs, then A is more likely than B. It's not enough to say "I've never heard of A happening" - you also need to consider whether you've ever heard of B happening. If you've never heard of either, then you need more data - which means more studying.
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sushant very good question

I tried to solve this.
See code:



When you run this program, you will get

Exception in thread "main" java.util.FormatterClosedException



and this FormatterClosedException is the child of IllegalStateException.


public class FormatterClosedException extends IllegalStateException



Here this exception is not thrown by JVM, it is thrown programmatically by the Formatter API developers, if you see the code of Formatter API, you will find this method get called.

private void ensureOpen() {
if (a == null)
throw new FormatterClosedException();
}



You can see the developer is throwing this exception that is the child of IllegalStateException. Any doubt ???
[ December 21, 2008: Message edited by: punit singh ]
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IllegalStateException here you will find lots of example where developers has used this exception.
 
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm...I did this question yesterday...
I only chose Number Format Exception and Illegal Argument Exception..and I'm pretty sure that Master Exam said I was correct!
 
Sushant Kaushik
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Punit & Mike. The explanation clears my doubt
 
reply
    Bookmark Topic Watch Topic
  • New Topic