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.
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.
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?
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)? - 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.