Skip to main content

Various possible combinations of try-catch-finally


Various possible combinations of try-catch-finally
Example 1:
      class Test1{
               public static void main(String[] args){
                try
                   {}
                catch(ArithmeticException e)
                   {}
                }
           }
Output:
Compile and running successfully.
Example 2:
     class Test1{
                public static void main(String[] args){
                     try
                        {}
                            catch(ArithmeticException e)
                            { }
                            catch(NullPointerException e)
                            {}
                        }
                }
Output:
Compile and running successfully.
Example 3:
       class Test1{
                 public static void main(String[] args){
                     try
                       {}
                       catch(ArithmeticException e)
                       {}
                      catch(ArithmeticException e)
                       {}
                  }
           }
Output:
Compile time error.
Test1.java:7: exception java.lang.ArithmeticException has already been caught
catch(ArithmeticException e)
Example 4:
     class Test1{
                 public static void main(String[] args){
                      try
                          {}
                    }
             }
Output:
Compile time error
Test1.java:3: 'try' without 'catch' or 'finally'
try
Example 5:
   class Test1{
          public static void main(String[] args){
             catch(Exception e)
              {}
          }
     }
Output:
Compile time error.
Test1.java:3: 'catch' without 'try'
catch(Exception e)
Example 6:
     class Test1{
           public static void main(String[] args){
                 try
                    {}
                        System.out.println("hello");
                 catch(Exception e)
                    {}
                }
          }
Output:
Compile time error.
Test1.java:3: 'try' without 'catch' or 'finally'
Try
Example 7:
      class Test1{
            public static void main(String[] args){
                  try
                     {}
                  catch(Exception e)
                     {}
                  finally
                     {}
                 }
          }
Output:
Compile and running successfully.  
Example 8:
        class Test1{
               public static void main(String[] args){
                    try
                         {}
                    finally
                         {}
                  }
         }
Output:
Compile and running successfully.
Example 9:
         class Test1{
                public static void main(String[] args){
                      try
                          {}
                      finally
                          {}
                     finally
                          {}
                  }
          }
Output:
Compile time error.
Test1.java:7: 'finally' without 'try'
Finally 
Example 10:
              class Test1{
                   public static void main(String[] args){
                       try
                         {}
                       catch(Exception e)
                         {}
                        System.out.println("hello");
                      finally
                           {}
                    }
            }
Output:
Compile time error.
Test1.java:8: 'finally' without 'try'
Finally
Example 11:
          class Test1{
                   public static void main(String[] args){
                          try
                                {}
                          finally
                               {}
                         catch(Exception e)
                               {}
                       }
             }
Output:
Compile time error.
Test1.java:7: 'catch' without 'try'
catch(Exception e)
Example 12:
            class Test1{
                    public static void main(String[] args){
                          finally
                             {}
                        }
              }
Output:
Test1.java:3: 'finally' without 'try'
Finally
Example 13:
            class Test1{
                  public static void main(String[] args){
                       try
                          { 
                              try{}
                             catch(Exception e){}
                           }
                      catch(Exception e)
                         {}
                    }
              }
Output:
Compile and running successfully. 
Example 14:
         class Test1{
                 public static void main(String[] args){
                      try
                        { }
                     catch(Exception e)
                        {
                             try{}
                             finally{}
                         }
                    }
              }
Output:
Compile and running successfully.
Example 15:
         class Test1{
                public static void main(String[] args){
                     try
                           { }
                           catch(Exception e)
                                {
                                    try{}
                                    catch(Exception e){}
                                }
                           finally{
                           finally{}
                       }
               }
        }
Output:
Compile time error.
Test1.java:11: 'finally' without 'try'
 finally{} 
Example 16:
            class Test1{
                   public static void main(String[] args){
                   finally{}
                   try{ } 
                   catch(Exception e){}
               }
          }
Output:
Compile time error.
Test1.java:3: 'finally' without 'try'
finally{}
Example 17:
           class Test1{
                  public static void main(String[] args){
                     try{ }
                     catch(Exception e){}
                     finally
                       {
                             try{}
                             catch(Exception e){}
                             finally{}
                       }
                }
        } 
Output: Compile and running successfully. 

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