/**
 * TestException.java -- this is an application that illustrates the use of
 * try-catch handling (exception handling) to make a program more
 * robust/reliable.
 *
 * CSCE 155 Fall 2005
 * @author Leen-Kiat Soh
 * @version 1.0
 */

import java.io.*;

class TestException {

   /**
    * This method is not robust.  It does not catch any exception even though
    * "Integer.parsetInt" potentially can generate an exception.  This method
    * could cause the program to crash.
    * @params inputStr a String as an input to the method.
    */

   public static int mysteryMethod(String inputStr) {

      int number = 0;
      number = Integer.parseInt(inputStr);
      System.out.println("Number entered is [" + number + "]");

      return number;
   }

   /**
    * A robust method that throws several possible exceptions.  Each exception
    * is caught properly. 
    * @params inputStr a String as the input to the method.
    */

   public static int mysteryMethodRobust(String inputStr) throws Exception {

      int number = 0;
      try {
         number = Integer.parseInt(inputStr);

         if (number > 100)
            throw new Exception("Number is bigger than 100");
        
      } catch (NumberFormatException evt) {
        System.out.println("Some numberFormatException has occurred.");
	System.out.println(evt.getMessage());
      } catch (Exception evt) {
	System.out.println(evt.getMessage());
      } finally {
         System.out.println("DONE!!!!");
      }

      return number;
      
   }

   /**
    * This method can call two different methods, one is robust, the other is
    * not.
    * If the method terminates normally, it will print out to the console
    * accordingly.
    */

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

      // int x = mysteryMethod(args[0]);
      int x = mysteryMethodRobust(args[0]);
      System.out.println("Program terminates normally - number is [" + x + "]");


   }


}



