Key point related to try, catch and finally

Key point related to try, catch and finally & finally block


    Key Point About (try/catch/finally) :

  • Each catch clause specifies the type of one exception, and provides a name for it (similar to the way a function header specifies the type and name of a parameter). Java exceptions are objects, so the statements in a catch clause can refer to the thrown exception object using the specified name.

  • The finally clause is optional

  • In general, there can be one or more catch clauses. If there is a finally clause, there can be zero catch clauses.

  • If catch statement not handled abnormal condition that control goes to out of execution.

  • Execution thrown during execution of the try block can catch and handled in a catch block.

  • One catch block handled only one abnormal condition.

  • Try block there can be zero or more catch block but only one finally block.

  • The try catch block finally block must always appear in conjunction with a try block in the above order.

  • Not any statement write between try catch finally block.

  • FINALLY :

  • Suppose we should execute some mandatory statements either “ Exception got or not, raised or not” at that time mandatory statements will be executed. For that purpose sun people introduced a block and it’s named as “finally” block.

  • A block named as finally it’s called as finally block.

  • Exception got or not , raised or not finally block will be executed.

  • finally block should be followed by catch block or try block.

  • Example 1:

     
                    
                 package com.sdj.str;
                 public class ExceptionFinally
                 {
                    public static void main(String[] args) 
                    {
                       System.out.println("connection start");
                       try
                       {
                          String str=args[0];
                          int i=Integer.parseInt(str);
                          int reuslt=i/(i-9);
                       }
                       finally
                       {
                          System.out.println("connection close");
                       }
                    }
                 }                

    Output:

         connection start
         
         
         connection close

    Explanation:

      At line number 9 we have an Exception
      i.e, java.lang.ArrayIndexOutOfBoundsException why because we can’t pass any cmdLine arguments. But finally block will be executed.

    Example 2:

     
                    
                 package com.sdj;
                 public class ExceptionFinally
                 {
                    public static void main(String[] args) 
                    {
                       System.out.println("connection start");
                       try
                       {
                          String str=args[0];
                          int i=Integer.parseInt(str);
                          int result=i/(i-9);
                       }
                       catch (ArrayIndexOutOfBoundsExceptionaie)
                       {
                          System.out.println("AIOBE exception");
                       }
                       finally
                       {
                          System.out.println("connection close");
                       }
                    }
                 }                

    Output:

    connection start aie exception connection close
    Note

    If we want to keep some statements in finally block then no need to keep same code inside some another blocks. Why because duplicate will be occurred.

    Example 3:

     
                    
                 package com.sdj;
                 public class ExceptionFinally
                 {
                    public static void main(String[] args) 
                    {
                       System.out.println("connection start");
                       try
                       {
                          String str=args[0];
                          int i=Integer.parseInt(str);
                          int reuslt=i/(i-9);
                       }
                       finally
                       {
                          System.out.println("connection close");
                       }
                    }
                 }                

    Output:

         connection start
         
         
         connection close

    Example 4:

     
                    
                 package com.sdj;
                 public class ExceptionFinally
                 {
                    public static void main(String[] args) 
                    {
                       System.out.println("connection start");
                       try 
                       {
                          String str=args[0];
                       }
                       finally 
                       {
                          System.out.println("connection close");
                       }
                       catch (ArrayIndexOutOfBoundsExceptionaie)
                       {
                          System.out.println("aie exception");
                       }
                    }
                 }                
    • At line 15 compilation fails why because finally shouldn’t be followed by catch block. It should after catch block.

    • If we use catch blocks it should be immediately followed by try block but not finally block.

    • Note

      try with finally and try with catch blocks is possible.

    Example 5:

     
                    
                 package com.sdj;
                 public class ExceptionFinally
                 {
                    public static void main(String[] args) 
                    {
                       try 
                       {
                          String str=args[0];
                       }
                       catch (ArrayIndexOutOfBoundsExceptionaie)
                       {
                          System.out.println("aie exception");
                       } 
                         
                       finally 
                       {
                          System.out.println("connection close");
                       }
                       finally 
                       {
                          System.out.println("connection close");
                       }
                    }
                 }                
    • At line number 19 compilation fails why because we can’t keep more than one finally block with a single try block.

    Example 6:

     
                    
                 package com.sdj;
                 public class ExceptionFinally
                 {
                    public static void main(String[] args) 
                    {
                       try 
                       {
                          String str=args[0];
                       }
                       catch (ArrayIndexOutOfBoundsExceptionaie)
                       {
                          System.out.println("aie exception");
                       }
                       finally
                       {
                          System.out.println("connection close");
                       }
                       catch (NumberFormatExceptionaie)
                       {
                          System.out.println("connection close");
                       }
                    }
                 }                
    • At line number 19 compilation fails why because if we have multiple catch blocks then finally should be placed after catch block.

    • catch block will not be followed by finally block.

    Example 7:

     
                    
                 package com.sdj;
                 public class ExceptionFinally
                 {
                    public static void main(String[] args) 
                    {
                       try 
                       {
                          String str=args[0];
                       }
                       catch (ArrayIndexOutOfBoundsExceptionaie)
                       {
                          System.out.println("aie exception");
                       }
                       finally
                       {
                          System.out.println("connection close");
                       }
                    }
                 }                
    • At line number 15 compilation fails why because without try block we can’t keep catch/finally block. Try block is mandatory.