• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

SC Exam Lab question 38 on exception

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I got it. Thank you very much Kenneth.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic