Pin It

Program to demonstrate working of finally block in Java

Program to demonstrate working of finally block in Java

package com.hubberspot.examples;

public class FinallyBlock {

   public static int f(int i) {
 System.out.println("Inside method f()");
 try{
           System.out.println("Print 1");
    if(i == 1)
  return 1;
    System.out.println("Print 2");
    if(i == 2)
  return 2;
    System.out.println("Print 3");
    if(i == 3)
  return 3;
    System.out.println("Print 4");
    if(i == 4)
  return 4;
  }finally{
      System.out.println("Always returns below value");
      return 5;
   }
 }
 
   public static void main(String[] args) {
  
       for(int i = 1; i < 5; i++)
       {
   System.out.println(f(i));   
       }

   }

}


Output of the program : 


 
Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot