question 5

Posted Thursday, September 03, 2009 by SacrosanctBlood in

What is the output of below program, why and what is the concept guiding
the output?

public class Test {
  public static void main(String[] args) {
    try {
      String str = null;
      doSomething(str);
      System.out.println("1");
    } catch (RuntimeException e) {
      System.out.println("2");
    }
  }
  public static void doSomething(String str) {
    try {
        System.out.println(str.length());
    } finally {
      return;
    }
  }
}



1 comment(s) to... “question 5”

1 comments:

SacrosanctBlood said...

Output is :

1



Though there exists a Null pointer exception in the doSomething method,
the finally block has ‘return’ in it signifying a normal termination of
the method. Hence, the code continues to print 1.



Post a Comment