Java finally is a block it is always executed irrespective of try and catch.
Java finally-block contains clean-up code such as closing connection, stream etc.

It is not possible to write finally-block alone . We must take try-catch-finally or try- finally , these two are the possibilities. If we take any other combination then we will get compile-time error saying finally without try block .

  • Syntax of try-catch-finally block and try-finally block


    try-catch-finally

    
    try
    {
    	//error prone statements
    }
    catch(Exception_Type e)
    {
    	//handling code
    }
    finally
    {
    /* code to execute always 
     such as closing connection or closing stream etc
    */	
    }


    try-finally

    
    try
    {
    	//error prone statements
    }
    finally
    {
    /* code to execute always 
     such as closing connection or closing stream etc
    */	
    }

  • using finally block

    Example)
    drag_handle
     
    //example of finally-block
    class Example {
        public static void main(String arr[]) { 
             System.out.println("Start..");
                try
                {
                    System.out.println(50/0);
    
                }catch(ArithmeticException e)
                {
                    System.out.println("Please don' t divide a number by 0");
                    e.printStackTrace();
                }   
                finally{
                    System.out.println("finally block...cleanup code");
                }
    
             System.out.println("rest of the code ......");
             System.out.println("End..");
        }        
    }
    
    Output:
    Start..
    Please don' t divide a number by 0
    java.lang.ArithmeticException: / by zero
    	at Example.main(Test.java:8)
    finally block...cleanup code
    rest of the code ......
    End..

    example of try-finally block.

    
    class Example {
        public static void main(String arr[]) { 
             System.out.println("Start..");
                try
                {
                    System.out.println(500/2);
    
                }
                finally{
                    System.out.println("finally block...cleanup code");
                }
    
             System.out.println("rest of the code ......");
             System.out.println("End..");
        }        
    }
    

    Output:
    Start..
    250
    finally block...cleanup code
    rest of the code ......
    End..
    
  • Important points:

    • For each try block there can be zero or more catch blocks, but only one finally block.

    • In only one case the finally block will not be executed if program exits(either by calling System.exit() or by causing a fatal error that causes the process to abort).


Example of using System.exit to skip the execution of finally block.
drag_handle
class Example {
    public static void main(String arr[]) { 
         System.out.println("Start..");
            try
            {
                System.out.println(50/2);
                System.out.println("try block");
                System.exit(41);// terminating the program

            }catch(ArithmeticException e)
            {
                System.out.println("Please don' t divide a number by 0");
                e.printStackTrace();
            }   
            finally{
                System.out.println("finally block...cleanup code");
            }

         System.out.println("rest of the code ......");
         System.out.println("End..");
    }        
}

Output:

Start..
25
try block

What next?

The next topic is throw keyword in java

Share this page