Java – Finally Block

Finally block:

  • Finally, block is used to close resources (file, database etc.) after use.
  • Finally block executes whether or not an exception raised.
class Code
{
      public static void main(String[] args)
      {
                  try
                  {
                              // int a = 10/5 ; -> try-finally blocks execute.
                              // int a = 10/0 ; -> catch-finally blocks execute.
                              System.out.println(“Try Block”);
                  }
                  catch (Exception e)
                  {
                              System.out.println(“Catch Block”);
                  }
                  finally
                  {
                              System.out.println(“Finally Block”);
                  }
      }
}

Why closing statements belongs to finally block?

  • Resource releasing logic must be executed whether or not an exception raised.
  • For example, in ATM Transaction – the machine should release the ATM card in success case or failure case.

Unchecked v/s Checked Exceptions:

Unchecked Exceptions:

  • The Child exceptions of RuntimeException are called Unchecked.
  • Unchecked exceptions occur when program not connected to any resource.
  • Handling unchecked exception is optional.
  • Compiler not giving any Error if we don’t handle Unchecked Exceptions.

Checked Exceptions:

  • If the exception is not child of RuntimeException is called Checked.
  • Checked Exceptions occur when program connected to other resource.
  • Handling these exceptions is mandatory.
  • If we don’t handle the exception – Compiler raise Error message.

General Syntax to Open and Close File or Database:(programs never compiles)

  • We close the resources in finally block
  • We need to check the resource is connected or not while closing it.
  • We use if block for this check as follows
Resource – FileResource – Database
static void main()
{
            File f = null;
            try
            {
                        f = new File(“abc.txt”);
                        print(“File opened”);
            }
            catch (Exception e)
            {
                        print(“No such file”);
            }
            finally
            {
                        if(f != null)
                        {
                                    f.close();
                                    print(“File closed”);
                        }
            }
}
static void main()
{
            Connection con = null;
            try
            {
                        con = DriverManager….;
                        print(“Connected);
            }
            catch (Exception e)
            {
                        print(“Connection Failed”);
            }
            finally
            {
                        if(con != null)
                        {
                                    con.close();
                                    print(“DB closed”);
                        }
            }
}

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top