February 21, 2014

Java Finally!



public class Hello {

 public static void hello() {
  try {
   System.out.println("hi");
   return;
  } catch (RuntimeException e) {
                        //------------
  } finally {
   System.out.println("finally");

  }
  System.out.println("after try catch");
 }

        public static void main(String[] args){
                hello();
                System.out.println("after hello in parent");
 
        }
}
 
 
What do you think it prints out?
hi
finally
after hello in parent

The finally block gets executed in all these cases. The point of executing finally block at the end is to allow you to release all the acquired resources.

No comments:

Post a Comment