Privacy Policy Terms Of Use. Copyright © 2006-2010 Java Tutorials and Examples.
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
- public class Q3{
- public static void main(String args[]){
- try{
- Demo d = new Demo();
- d.method1();
- System.out.println("Main Completed");
- }catch(Exception e){}
- }
- }
- class Demo{
- public void method2(){
- throw new ArrayIndexOutOfBoundsException();
- }
- public void method1(){
- try{
- method2();
- }catch(NullPointerException ae){
- System.out.println("Exception caught");
- }finally{
- System.out.println("Method 1 ends");
- }
- }
- }
b.Program will print "Method 1 ends"
c. Program will print "Exception Caught"
d. Program will print Main Completed
Answer:
Correct choice is B - program will print Method 1 ends. Method 1 calls the method 2 which throws ArrayIndexOutOfBoundsException, but that is not caught anywhere in the method 1. So, this excpetion will be thrown again to the calling main method and will be caught in main method's catch block. However, before program exits, it goes in the finally block of method 1 and prints "Method 1 ends". Remember, finally block is ALWAYS executed in normal scenario in the event of exception or not.

Post new comment