Finally Block In Java

Finally, Block In Java: In our previous post, we discussed and learned about Exception Handling and how to use the try-catch block. In this post, we will learn about the finally block, how to use the finally block with a try-catch block, and different scenarios and uses of finally with simple Java programs.

As we shared in our previous post, we need to put all the important statements in the Finally block, which should be executed whether an exception occurs, for example, closing a connection to a database or file, etc.

Syntax of Finally block

try 
{
   //Statements that may cause an exception
}
catch 
{
   //Handling exception
}
finally 
{
   //Statements to be executed
}

Finally, Block example

class Example
{
   public static void main(String args[]) 
   {
      try
      {
         int num=121/0;
         System.out.println(num);
      }
      catch(ArithmeticException e)
      {
         System.out.println("Number should not be divided by zero");
      }
      /* Finally block will always execute
       * even if there is no exception in try block
       */
      finally
      {
         System.out.println("This is finally block");
      }
      System.out.println("Out of try-catch-finally");
   }
}

Points to Remember Regarding Finally

  • Finally, the block is optional because we can handle an exception using the try-catch block.
  • A Finally block must be associated with a try block, which means that in a program, you can not write a Finally block without a try block.
  • Those statements are written inside the Finally block that will be executed irrespective of whether an exception occurs.
  • The execution flow of the Finally block in the normal case is it will execute after the try block, and if there is some exception, then the first try block catch block, and after that, finally, the block will be executed.
  • If an exception occurs in the Finally block, it behaves like another exception, and you will get a system-generated error message.
  • If there are any control transfer statements present, like return, break, and continue in the try block, at last, the Finally block will be executed.

Finally, Block with Return Statement

class JavaFinally
{
   public static void main(String args[])
   {
      System.out.println(JavaFinally.myMethod());
   }
   public static int myMethod()
   {
      try 
      {
         return 112;
      }
      finally 
      {
         System.out.println("This is Finally block");
         System.out.println("Finally block ran even after return statement");
      }
   }
}

Cases when the Finally block doesn’t execute

There are also some circumstances in what time the Finally block is executed, and the circumstances are below:

  • The Death of an Execution thread
  • If there is a system.exit() method in any block
  • If an exception occurs in the Finally block

How do you handle exceptions using the Try Finally In Java example?

package com.softwaretestingblog.programs;
public class WithTryFinally {
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      try
      {
         int i=9;
         int z=i/0;
         System.out.println("Executed Sucessfully"+ z);
      }
      finally
      {
         System.out.println("Exception Comes");
      }
   }
}

Output:

Exception Comes
Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.softwaretestingblog.programs.WithTryFinally.main(WithTryFinally.java:8)

How to Use Try, Catch, & Finally-Exception With Java Program?

package com.softwaretestingblog.programs;
public class WithTryCatchFinally {
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      try
      {
         int i=8;
         int z=i/0;
         System.out.println("Executed Sucessfully"+z);
      }
      catch(ArithmeticException e)
      {
         System.out.println(e.getMessage());
      }
      finally
      {
         System.out.println("Finally Block Executed");
      }
   }
}

Output:

/ by zero
Finally Block Executed

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment