/*******************************************************************
** This application is an illustration of how the Exceptional
** handing works and it shows the throws, try, catch and finally
** clauses plus the execution sequence of statements.
********************************************************************/
public class ExceptionWithFinally
{
  final int   ZERO = 0;
  int         ExecutionCount = 0;

  public ExceptionWithFinally()
  {
    Method_1();
    System.out.println("===========================================");
    Method_2();
  }
  //**********************************************
  float  CatchDividByZeroMethod(int PassedInteger, int DividByInteger)
        throws ArithmeticException
  {
    ExecutionCount++;
    System.out.println(ExecutionCount
                  + ")***  The start of CatchZeroDivisionMethod   ****");
    if(0 == DividByInteger)
      throw new ArithmeticException();
    ExecutionCount++;
    System.out.println(ExecutionCount
                  + ")***    The end of CatchZeroDivisionMethod   ****");
    return (PassedInteger / DividByInteger);
  }  // CatchZeroDivisionMethod
  //**********************************************
  void Method_1()
  {
    int         DividByValue = ZERO;
    int         IntegerValue = 20;
    float       FloatValue   = -20;

    ExecutionCount++;
    System.out.println(ExecutionCount
                  + ")***  The start of Method_1   ****");
    ExecutionCount++;
    System.out.println(ExecutionCount
                  + ")FloatValue = \t" + FloatValue);
    try
    {
      ExecutionCount++;
      System.out.println(ExecutionCount
                  + ")***  Start of Method_1 Try Clause  ****");
      FloatValue = CatchDividByZeroMethod(IntegerValue, DividByValue);
      return;
    }
    catch (ArithmeticException  MyArithmeticException)
    {
      ExecutionCount++;
      System.out.println(ExecutionCount
                  + ")*** Start of Method_1 Catch clause  ****");
      ExecutionCount++;
      System.out.println(ExecutionCount
                  + ")***  can not divid by ZERO!!!! ****");
      return;
    }
    finally
    {
      ExecutionCount++;
      System.out.println(ExecutionCount
                  + ")*** Start of Method_1 finally clause  ****");
    }
/**
****************************************************************
** This portion of the code will not be reached and the compiler
** will not compile unless the code is commented.
****************************************************************
    ExecutionCount++;
    System.out.println(ExecutionCount
                  + "**** After Method_1 finally Block *****");
    ExecutionCount++;
    System.out.println(ExecutionCount
                  + ")FloatValue = \t" + FloatValue);
*********/
  } // Method_1
  //**********************************************
  void Method_2()
  {
    int         DividByValue = 2;
    int         IntegerValue = 50;
    float       FloatValue   = -50;

    ExecutionCount++;
    System.out.println(ExecutionCount
                  + ")***  The start of Method_2   ****");
    ExecutionCount++;
    System.out.println(ExecutionCount
                  + ")FloatValue = \t" + FloatValue);
    try
    {
      ExecutionCount++;
      System.out.println(ExecutionCount
                  + ")***  Start of Method_2 Try Clause  ****");
      FloatValue = CatchDividByZeroMethod(IntegerValue, DividByValue);
    }
    catch (ArithmeticException  MyArithmeticException)
    {
      ExecutionCount++;
      System.out.println(ExecutionCount
                  + ")*** Start of Method_2 Catch clause  ****");
      ExecutionCount++;
      System.out.println(ExecutionCount + ")***  can not divid by ZERO!!!! ****");
      return;
    }
    finally
    {
      ExecutionCount++;
      System.out.println(ExecutionCount
                  + ")*** Start of Method_2 finally clause  ****");
    }
    ExecutionCount++;
    System.out.println(ExecutionCount
                  + ")**** After Method_2 finally Block *****");
    ExecutionCount++;
    System.out.println(ExecutionCount
                  + ")FloatValue = \t" + FloatValue);
    ExecutionCount++;
    System.out.println(ExecutionCount
                  + ")***  The End of Method_2   ****");
  } // Method_2
  /****************************************************/
  public static void main(String [] args)
  {
    ExceptionWithFinally  MyExceptionWithFinally
          = new ExceptionWithFinally();
	try
	{
		System.in.read();
	}
	catch(Exception myException)
	{// do nothing
	}
  }
}
