Skip to main content

Finally


Finally block:
It is never recommended to take clean up code inside try block because there is no guarantee for the execution of every statement inside a try.
It is never recommended to place clean up code inside the catch block because if there is no exception then catch block won’t be executed.
We require someplace to maintain clean up code which should be executed always irrespective of whether exception raised or not raised and whether handled or not handled such type of place is nothing but finally block.
Hence the main objective of finally block is to maintain cleanup code.
Example:
          try
              {
                 risky code
              }
              catch(x e)
               {
                     handling code
               }
             finally
                 {
                        cleanup code
                  }
The specialty of finally block is it will be executed always irrespective of whether the exception raised or not raised and whether handled or not handled.
Example 1:
         class Test
           {
                  public static void main(String[] args)
                      {
                            try
                                 {
                                     System.out.println("try block executed");
                                   }
                            catch(ArithmeticException e)
                                {
                                       System.out.println("catch block executed");
                                }
                         finally
                                  {
                                        System.out.println("finally block executed");
                                   }
                       }
           }
Output:
Try block executed
Finally block executed
Example 2:
     class Test
           {
               public static void main(String[] args)
                      {
                             try
                                 {
                                      System.out.println("try block executed");
                                      System.out.println(10/0);
                                  }
                                  catch(ArithmeticException e)
                                    {
                                          System.out.println("catch block executed");
                                   }
                                   finally
                                        {
                                                 System.out.println("finally block executed");
                                         }
                          }
                  }
Output:
Try block executed
Catch block executed
Finally block executed
Example 3:
          class Test
            {
                public static void main(String[] args)
                   {
                       try 
                          {
                               System.out.println("try block executed");
                               System.out.println(10/0);
                         }
                          catch(NullPointerException e)
                             {
                                   System.out.println("catch block executed");
                             }
                          finally
                            {
                               System.out.println("finally block executed");
                             }
                      }
             } 
Output:
Try block executed
Finally block executed
Exception in thread "main" java.lang.ArithmeticException: / by zero at Test.main(Test.java:8)
Return Vs Finally:
Even though return present in try or catch blocks first finally will be executed and after that only return statement will be considered that is finally block dominates return statement. 
Example:
       class Test
            {
                 public static void main(String[] args)
                      {
                             try
                                   {
                                        System.out.println("try block executed");
                                             return;
                                  }
                           catch(ArithmeticException e)
                                {
                                          System.out.println("catch block executed");
                                 }
                         finally
                               {
                                    System.out.println("finally block executed"); 
                               }
                     }
            }
Output:
Try block executed
Finally block executed
If return statement present try catch and finally blocks then finally block return statement will be considered.
Example:
      class Test
          {
                 public static void main(String[] args)
                     { 
                           System.out.println(methodOne());
                     }
                public static int methodOne()
                   {
                       try
                           {
                                System.out.println(10/0);
                                return 777;
                            }
                      catch(ArithmeticException e)
                           {
                                  return 888;
                           }
                      finally
                               {
                                      return 999;
                                }
                    }
           }
Output:
999
• There is only one situation where the finally block won’t be executed is whenever we are using System.exit(0) method. 
Example:
      class Test
            {
                   public static void main(String[] args)
                       {
                              try
                                    {
                                           System.out.println("try"); 
                                           System.exit(0);
                                    }
                            catch(ArithmeticException e)
                                {
                                    System.out.println("catch block executed");
                                }      
                           finally
                                {
                                         System.out.println("finally block executed");
                                 }
                          }
              }
Output:
Try 

Comments

Popular posts from this blog

Core Java

Content 1 - Language fundamental   Identifier Reserved words Data types Literals Arrays Types of variables Var arg method Main method Java coding standards 2 -  Declaration and Access Modifiers Java source file structure Class modifiers Member modifiers Interfaces 3 - Exception Handling Introduction Runtime stack mechanism Default exception handling in java Exception hierarchy Customized exception handling by try-catch  Control flow in try-catch Methods to print exception information Try with multiple catch blocks Finally Difference between final, finally, finalize Control flow in try-catch-finally Control flow in nested try-catch-finally Various possible combinations of try-catch-finally throw keyword throws keyword Exception handling keywords summary Various possible compile-time errors in exception handling Customized exceptions Top-10 exceptions  4 - String Concept of String Immutable String String Comparison String Conca