A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

Finally Block

Finally Block

finally

  • The finally block always executes no matter what happens in try block regardless exception in handle or not.

  • In the try block we place that code that frequently used to close the resources that were opened in the try block.

  • This block is optional to use.

Syntax-





Syntax:-
try 
{
   //code to open file
}

catch(Exception e) 
{
   //code to handle exception while reading the file
}

finally
{
   //close the file
}


 

program 1 by using finally :-





class Test1
{
   public static void main(String args[]) 
   {
      try 
      {
         /*This statement is suspectable the code can throw exception
         *we handled this block statement by placing these statements
         *inside try block and handled the exception in catch block*/
         
         int num1 = 0;
         int num2 = 20 / num1;
         System.out.println(num2);
         System.out.println("Rest of the try code...");
      }

      catch (ArithmeticException e) 
      { 
        /*This block will only execute to handle Arithmetic exception that will occurs in try block.*/
         
         System.out.println("We can not divide any number by zero..");
      }
  
      finally
      {
         System.out.println("This is finally block that always Execute");
      }  
  
      System.out.println("Rest of Without Exception Code...");
   }
}
OutPut:-
We can not divide any number by zero..
This is finally block that always Execute
Rest of Without Exception Code...


 

program 2 by using finally with try without catch :-





class Test1
{
   public static void main(String args[]) 
   {
      try 
      {
         /*This statement is suspectable the code can throw exception
         *we handled this block statement by placing these statements
         *inside try block and handled the exception in catch block*/
         
         int num1 = 0;
         int num2 = 20 / num1;
         System.out.println(num2);
         System.out.println("Rest of the try code...");
      }

      finally
      {
         System.out.println("This is finally block that always Execute");
      }  
  
      System.out.println("Rest of Without Exception Code...");
   }
}
OutPut:-
This is finally block that always Execute
Exception in thread "main" java.lang.ArithmeticException: / by zero
        at Test1.main(TryProgram.java:12)


 

When can finally will not Execute

  • System. exit() method.

  • If an exception arising in the finally block.

Finally block and System.exit():-

program 1 by using finally :-





Finally block Without Exception code:-
class Test1
{
   public static void main(String args[]) 
   {
      try 
      {
         System.out.println("Rest of the try code...");
         System.exit(0);

      }
      catch (Exception exp) 
      {
         System.out.println(exp);
   
      }

  
      finally
      {
         System.out.println("This is finally block that always Execute");
      }  
  
      System.out.println("Rest of Without Exception Code...");
   }
}


OutPut:-
Rest of the try code...


 

Finally block with Exception code:-





class Test1
{
   public static void main(String args[]) 
   {
      try 
      {
         /*This statement is suspectable the code can throw exception
          *we handled this block statement by placing these statements
          *inside try block and handled the exception in catch block*/
          
         int num1 = 0;
         int num2 = 20 / num1;
         System.out.println(num2);
   
         System.out.println("Rest of the try code...");
   
      }
      catch (Exception exp) 
      {
         System.out.println(exp);
         System.exit(0);
      }

  
      finally
      {
         System.out.println("This is finally block that always Execute");
      }  
  
      System.out.println("Rest of Without Exception Code...");
   }
}
OutPut:-
java.lang.ArithmeticException: / by zero


 











LEARN TUTORIALS

.

.