/**
 * TestException.java -- this is an application that illustrates the use of
 * try-catch handling (exception handling) specifically on the impact of
 * using 'finally'.
 *
 * RAIK 183H Fall 2009
 * @author Leen-Kiat Soh
 * @version 1.0
 */

import java.io.*;

class TestExceptionFinally {

   /**
    * This method calls methodB and catches any exception.
    */

   public static void methodA(String inputStr)  {

      try {
         methodB(inputStr);
      } catch (Exception e)  {
         System.out.println("exception is caught here in A");
      }

   }  // end methodA


   /**
    * This method can potentially throw two exceptions.  One is just the
    * generic Exception when x < 10. Another is the parseInt exception if the
    * string to parseInt has non-digits.
    * However, the method only catches "NumberFormatException" generated by
    * parseInt, and propagates other exceptions.
    *
    * Then, we have two statements ... one in "finally", and one after
    * "finally".  
    *
    * Now, if an exception is thrown, which statement will be printed out?
    *
    * Now, do you see why we have the "finally" block to take care of all the
    * loose ends when an exception occurs?
    *
    */

   public static void methodB(String inputStr) throws Exception {

      try {

         int x = Integer.parseInt(inputStr);
         if (x < 10) 
            throw new Exception("some exception!");
            
      } catch (NumberFormatException e) {
         System.out.println("exception is caught");
      } finally {
         System.out.println("this is a statement *in* finally");
      }

      System.out.println("this is a statement *after* finally");

      
   }  // end methodB

   public static void main(String[] args) throws Exception {

      methodA(args[0]);  // call methodA with the input commandline argument.

   }  // end main


}



