Skip to main content

Control flow in nested try-catch-finally


Control flow in nested try-catch finally:
Example:
     class Test
           {
                public static void main(String[] args)
                    {
                    try
                         {
                              System.out.println("statement1");
                              System.out.println("statement2");
                              System.out.println("statement3");
                             try
                                  {
                                         System.out.println("statement4");
                                         System.out.println("statement5");
                                         System.out.println("statement6");
                                  }
                            catch(ArithmeticException e)
                                   {
                                          System.out.println("statement7");
                                   }
                            finally
                                  {
                                      System.out.println("statement8");
                                   }
                                      System.out.println("statement9");
                           }
                          catch(Exception e)
                             {
                                     System.out.println("statement10");
                              }
                          finally
                                {
                                       System.out.println("statement11");
                                }
             System.out.println("statement12");
}
}
Case 1: if there is no exception. 1, 2, 3, 4, 5, 6, 8, 9, 11, 12 normal termination.
Case 2: if an exception raised at statement 2 and corresponding catch block matched 1,10,11,12 normal terminations.
Case 3: if an exception raised at statement 2 and the corresponding catch block is not matched 1, 11 abnormal termination.
Case 4: if an exception raised at statement 5 and corresponding inner catch has matched 1, 2,3, 4, 7, 8, 9, 11, 12 normal terminations.
Case 5: if an exception raised at statement 5 and inner catch has not matched but the outer catch block has matched. 1, 2, 3, 4, 8, 10, 11, 12 normal termination.
Case 6: if an exception raised at statement 5 and both inner and outer catch blocks are not matched. 1, 2, 3, 4, 8, 11 abnormal termination.
Case 7: if an exception raised at statement 7 and the corresponding catch block matched 1, 2, 3,.,.,., 8, 10, 11, 12 normal terminations.
Case 8: if an exception raised at statement 7 and the corresponding catch block not matched 1, 2, 3,.,.,.,8,11 abnormal terminations.
Case 9: if an exception raised at statement 8 and the corresponding catch block has matched 1,2, 3,.,.,.,., 10, 11,12 normal termination.
Case 10: if an exception raised at statement 8 and the corresponding catch block not matched1, 2, 3,.,.,.,., 11 abnormal terminations.
Case 11: if an exception raised at statement 9 and corresponding catch block matched 1, 2,3,.,.,.,., 8,10,11,12 normal termination.
Case 12: if an exception raised at statement 9 and corresponding catch block not matched 1, 2,3,.,.,.,., 8, 11 abnormal terminations.
Case 13: if an exception raised at statement 10 is always abnormal termination but before that finally, block 11 will be executed.
Case 14: if an exception raised at statement 11 or 12 is always abnormal termination.
Note: if we are not entering into the try block then the finally block won’t be executed. Once we entered into the try block without executing finally block we can’t come out.
Example:
       class Test
            {
                public static void main(String[] args){
                     try{
                            System.out.println(10/0);
                          }
                   catch(ArithmeticException e){
                             System.out.println(10/0);
                      }
                 finally{
                             String s=null;
                             System.out.println(s.length());
                    }
               }
          }
Note: Default exception handler can handle only one exception at a time and that is the most recently raised exception. 

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