Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Recognizing checked exceptions. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Recognizing checked exceptions." Watch "Recognizing checked exceptions." New topic
Author

Recognizing checked exceptions.

Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 157

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


OCPJP 6, OCMJD (2/3)
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
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

Joined: Jul 13, 2011
Posts: 157

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

Joined: Dec 08, 2010
Posts: 1295

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.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD)
Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 157

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

Joined: Dec 08, 2010
Posts: 1295

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

Joined: Jan 15, 2012
Posts: 60
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

Joined: Dec 08, 2010
Posts: 1295

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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Recognizing checked exceptions.
 
Similar Threads
Checked Exceptions
Checked and Unchecked exceptions
compiler question help
How can we identify checked and Unchecked exceptions ?
Exceptions