wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes SC Exam Lab question 38 on exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "SC Exam Lab question 38 on exception" Watch "SC Exam Lab question 38 on exception" New topic
Author

SC Exam Lab question 38 on exception

Edmen Tay
Ranch Hand

Joined: Oct 21, 2008
Posts: 39
Hi, i got this question from SC Exam Lab Simulator, Exam 2 - Question 38

public class Asemble{
public static void main(String args[]){
try{
go1();
}catch(Exception e){}
finally{
System.out.println("B");
}
}
public static void go1()throws ArrayIndexOutOfBoundsException{go2();}
public static void go2()throws ArithmeticException{go3();}
public static void go3()throws InterruptedException{go4();}
public static void go4()throws NullPointerException{
System.out.println("A");
throw new ArrayIndexOutOfBoundsException();
}
}

Answer: Compilation fails

Explanation:
the method go3() can throw the checked exception InterruptedException

if you want to call to the method go3(), the calling statement should be wrapped inside an appropriate 'try' block, or the calling method should be declared to "throws InterruptedException" or super type of InterruptedException.

But the method go2() attempts to call to the go3() method, without fulfilling above requirement. therefore a compile time error will be generated.

I still can't get the explanation point. Please advice.

Thanks

Regards,
Edmen
Massimo Battestini
Greenhorn

Joined: Nov 21, 2008
Posts: 11
For what I have understood:

the go2() method is launching the go3() method which can throw an InterruptedException, but go2() won't be able to tackle this exception; in fact go2() can throw ArithmeticException only (which is not a supertype of InterruptedException) nor doesn't have any try/catch block.

Hope this helps....
Kenneth Lomvey
Ranch Hand

Joined: Nov 08, 2008
Posts: 94
First at all, please make sure that you know about checked exceptions. If you don't know about that, you have to learn more about exception handling. Because that is a very important part in exception handling. If you already know about checked/unchecked exceptions, consider the following example:



In the above method, ArrayIndexOutOfBoundsException is an unchecked exception, therefore you CAN call to this method from anywhere, just like



But, if ArrayIndexOutOfBoundsException has been replaced with InterruptedException, which is a checked exception, you can NOT call to the above onMessage() method as above. To do this, you have to modify the calling statement as:



or



Instead of InterruptedException, you can use any super type of InterruptedException (unless java.lang.Object type)

This is what that explanation said.
Edmen Tay
Ranch Hand

Joined: Oct 21, 2008
Posts: 39
As I know:

Unchecked Excepton is the RuntimeException, and RuntimeException is subclass of Exception.

Meaning, InterruptedException is not subclass of RuntimeException?

For such question we need to identify the exception is subclass of Checked/Unchecked Exception at first. Am I correct?

Thanks.

Regards,
Edmen
Kenneth Lomvey
Ranch Hand

Joined: Nov 08, 2008
Posts: 94
Originally posted by Edmen Tay:
Meaning, InterruptedException is not subclass of RuntimeException?


You are correct.

InterruptedException is-a Exception
InterruptedException is-not-a RuntimeException

hence, InterruptedException is a checked exception.
Edmen Tay
Ranch Hand

Joined: Oct 21, 2008
Posts: 39
Ok, I got it. Thank you very much Kenneth.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SC Exam Lab question 38 on exception
 
Similar Threads
Stack overflow exception
Any Solution ?
ExamLab Thread question
Bug in question 13 Self Test 3 Book Kathy Sierra (SCJP6)
Any Solution of Deadlock?