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

JavaCaps Mock #2 Q60 on exceptions

 
Larry Lecomte
Ranch Hand
Posts: 37
  • 0
  • 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
  • 0
  • 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...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic