Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

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 : 


 
 
© 2021 Learn Java by Examples Template by Hubberspot