Login    New User    Forgot Password    Help      

The Finally Keyword

Java Tutorials

The finally keyword is used to create a block of code that follows a try block. A finally block of code always executes, whether or not an exception has occurred.

Using a finally block allows you to run any cleanup-type statements that you want to execute, no matter what happens in the protected code.

A finally block appears at the end of the catch blocks and has the following syntax:

Syntax

try
{
//Protected code
}catch(ExceptionType1 e1)
{
//Catch block
}catch(ExceptionType2 e2)
{
//Catch block
}catch(ExceptionType3 e3)
{
//Catch block
}finally
{
//The finally block always executes.
}

Example

// Finally
/* Finally crates a block of code that will be executed after a try/catch block has completed and before the code following the try/catch block
if a finally block is associated with a try ,the finally block will be executed upon conclusion of the try.
*/

class  ex6
{
static void excall()
      {
        try
        {
        System.out.println("Inside first function ");
        throw new RuntimeException(" Run Time Error ");
        }
        finally
        {
        System.out.println("Funcion is complete ");
        }
      }

static void excall1()
      {
        try
        {
        System.out.println("Inside second function ");
        return;
        }
        finally
        {
        System.out.println("Funcion is complete ");
        }

      }
static void excall2()
      {
        try
        {
        System.out.println("Inside Third function ");
        }
        finally
        {
        System.out.println("Funcion is complete ");
        }

      }

 

public static void main(String args[])
 {
        try
          {
          excall();
          }
        catch(Exception e)
         {
          System.out.println("Exception Caught  " +  e);
          }
         excall1();
         excall2();
 }
}

 

Output

Points to remember

  • must include either one catch clause or a finally clause
  • can be multiple catch clauses but only one finally clause
  • the try statements are executed until an exception is thrown or it completes successfully
  • a compile-error occurs if the code included in the try statement will never throw one of the caught checkedexceptions (runtime exceptions never need to be caught)
  • if an exception is thrown, each catch clause is inspected in turn for a type to which the exception can be assigned; be sure to order them from most specific to least specific
  • when a match is found, the exception object is assigned to the identifier and the catch statements are executed
  • if no matching catch clause is found, the exception percolates up to any outer try block that may handle it
  • a catch clause may throw another exception
  • if a finally clause is included, it's statements are executed after all other try-catch processing is complete
  • the finally clause executes wether or not an exception is thrown or a break or continue are encountered

Java Tutorials