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

SC Exam Lab question 38 on exception

 
Edmen Tay
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 94
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 94
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I got it. Thank you very much Kenneth.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic