This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Recognizing checked exceptions.

 
Glen Iris
Ranch Hand
Posts: 168
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Guys,

While working through some sample exam questions, I realised that I was weak at Exceptions.

I have just now, re-read the section on Exceptions in Chapter 5 of the Sierra Bates book and I still have this main problem:

How can you tell if an exception is Checked or Unchecked?

I realise that unless an exception extends RuntimeException, it is a Checked exception. However, I still cannot tell, just by an exceptions name, if it is a subclass of RuntimeException or not.

Is there a thought process to go through while deciding if an exception is checked or unchecked?

Alternativley, is there a definitive list of checked exceptions that theexam may throw up - if so, I can learn off this list and then conclude that if I do not recognise the exception, it must be a subclass of RuntimeException.

Thanks
 
Helen Ma
Ranch Hand
Posts: 451
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Glen. Yes Runtime exception or its subtypes are unchecked exceptions. For example, arithmeticException is thrown when you declare int a = 8/ 0 .This arithmetic exception is runtime exception and you don't need to put it in try/catch block or declare it in the method.



Visit this web site :http://docs.oracle.com/javase/6/docs/api/ and search for java.lang.RuntimeException. There will show up a list of unchecked exception. For the example, may be, you only need to know Null pointer exception, arithmetic exception, illegal argument exception, array index out of bound.


According to KB, error is unchecked similar to runtime exception.

From the web site, look for java.lang.Exception. All exceptions other than RuntimeException are checked exceptions.

In the exam, the most common checked exceptions you need to know are IOException or FileNotFoundException.
From the practice exam, the questions provide you which exception is checked and which one is the subtype of the exception.So, you don't need to worry about it.

 
Glen Iris
Ranch Hand
Posts: 168
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Helen,

I am still slightly confused. Can someone who has passed the exam please tell me the thought process to go through while deciding if an exception is checked or unchecked?

Did you just learn all of the subclasses of Runtime Exceptions?
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glen Iris wrote:Can someone who has passed the exam please tell me the thought process to go through while deciding if an exception is checked or unchecked?

Well, as per my knowledge, you don't get a question like - identify which among below are checked and unchecked exceptions. Further,
Helen Ma wrote:In the exam, the most common checked exceptions you need to know are IOException or FileNotFoundException.

and
Helen Ma wrote:All exceptions other than RuntimeException are checked exceptions.

Please let us know what exactly is your confusion.

I hope this helps.
 
Glen Iris
Ranch Hand
Posts: 168
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Anayonkar Shivalkar for your reply.

My worry is that I will get an exam question with complex code. The solution will have to do with an exception not being caught or declared. At the moment I am not sure how to recognize code that requires a checked exception, therefore I would probably end up looking for some other flaw in the code.

I need to know how to recognize a checked exception.

So are there any other checked exceptions that have appeared on the exam apart from IOException or FileNotFoundException?
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glen,

As Helen has mentioned, if there are any checked exceptions, question will clearly mention them (e.g. user defined exception etc.)

Now, regarding unchecked exception -
At first glance, question might not be seem to be related to exceptions at all, but you'll have to check some basic things - is code invoking a method on non-initialized object? - it will throw NullPointerException. Is code accessing an array index greater than allowed (say array is of 5 elements and code is accessing something like arr[5])? - there will be ArrayIndexOutOfBoundsException. If reference of one type has been casted to another type, but the casting is not valid, it will throw ClassCastException, and so on. I guess you got the idea.

Checked exception -
Here, question will mention which are checked exceptions. However, just because question mentions this, doesn't mean that it is exception related question (it might be something very basic - like a variable is defined in a loop, but is accessed outside loop etc.). That apart, checked exception questions are something like a method throws an exception, but invocation is in wrong try catch block, or method throws exception A, but within the method, there is some code saying 'throw new B();' - here you need to check if B IS-A A. Or it might be that base method throws exception A and overriding method throws exception B and those are altogether different exceptions etc. Again, I guess you got the idea.

I hope this helps.

All the best.
 
ayush raj
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can somebody tell me where can i find the exact hierarchy of the Exception and its related class ? Even i get confused while seeing an exception question .
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ayush raj wrote:Can somebody tell me where can i find the exact hierarchy of the Exception and its related class ?

You can find it here
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic