• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Caps Mock

 
Vijay Narayanan
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the result when you compile and run the following code?
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 throwMethod. followed by caught:
java.lang.IllegalAccessExcption: demo

The result given is A but i feel it should be D
any comments...
 
Tony Sam
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
When you throw an exception in a method,the exception must be declared to be thrown,such as :
static void throwMethod() throws IllegalAccessException{}
hope this can helps
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic