Skip to main content

Control flow in try-catch-finally


Control flow in 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");
                           }
                      catch(Exception e)
                          {
                                 System.out.println("statement4");
                          }
                      finally
                          {
                              System.out.println("statement5");
                           }
                            System.out.println("statement6");
                  }
        }
Case 1: If there is no exception. 1, 2, 3, 5, 6 normal termination.
Case 2: if an exception raised at statement 2 and corresponding catch block matched. 1,4,5,6 normal terminations.
Case 3: if an exception raised at statement 2 and the corresponding catch block is not matched. 1,5 abnormal termination.
Case 4: if an exception raised at statement 4 then it’s always abnormal termination but before the final block will be executed.
Case 5: if an exception raised at statement 5 or statement 6 its always abnormal termination. 

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