Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JavaCap no. 10

 
may leung
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anyone can help to solve this out?
public class ThrowsDemo {
static void throwMethod() {
System.out.println("Inside throwMethod.");
throw new IllegalAccessException("demo");
}
public static void main(String args[]) {
try {
throwMethod();
} catch (IllegalAccessException e) {
System.out.println("Caught " + e);
}
}
}
A) Compilation error
B) Runtime error
C) Compile successfully, nothing is printed.
D) inside demoMethod. followed by caught: java.lang.IllegalAccessExcption: demo

ans. is A But I don't understand!
Thank you
 
Aniruddha Mukhopadhyay
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi May,
It cannot compile because method throwMethod() has lines "throw new IllegalAccessException("demo")" in it's code, but does not declare that in the beginning. It can be made to compile by making declaration of method to start by "static void throwMethod() throws IllegalAccessException{..."
Aniruddha
 
may leung
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic