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

JavaCaps Mock #2 Q60 on exceptions

 
Larry Lecomte
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class ThrowsDemo {
static void throwMethod() throws Exception {
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 throwMethod. followed by caught: java.lang.IllegalAccessException: demo
Answer is: A
But in JLS�11.2 it says:
For each checked exception which is a possible result, the throws clause for the method (�8.4.4) or constructor (�8.8.4) must mention the class of that exception or one of the superclasses of the class of that exception.
Isn't Exception a superclass of IllegalAccessException
 
Larry Lecomte
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I found that the catch clause must declare
that it catches objects of type Exception because of the declaration of throwMethod. The problem was NOT in the throwMethod...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic