/**
 * Example of throwing and catching Exceptions
 * in this case we are raising our own exception class called "NegativeException"
 *
 * Also if argument is not a number, we print a program stack trace 
 *
 * Example of entries that would cause exceptions:
 *      java Except3        --> Missing arguments 
 *      java Except3  AA  3 --> Argument not a number + (program stack trace)
 *      java Except3  10  0 --> Cannot calculate average - count = 0
 *      java Except3  10 -2 --> Denominator cannot be negative   
 *
 * ** Also please note that this class instantiate itself
 */

public class Except4
{
    public static void main(String[ ] args)
    {
        //---- Check for entry of at least 2 arguments (total and count) ----------

        //in this try block, we check to make sure that client entered 2 arguments. 
        //If not, we throw an Exception

        try {
            if (args.length != 2)
                throw new Exception( );
        }
        catch (Exception e) {
            System.out.println("Missing args - Re-enter with total and count");
            System.exit(0);
        }


        Except4 pgm = new Except4( );            //instantiate a new pgm object

        double avg  = pgm.calcAvg(args);         //call object's calcAvg method

        System.out.println("The average is: " + avg);
    }
    

/**
 * Method to calculate average
 *
 */
    double calcAvg(String[ ] args)				//instance method 
    {  

        //---- Try converting args[0] to double, and args[1] to int ---------------

        //in this try block, an entry of anything other that a double and an integer 
        //will cause Java to automatically throw and Exception.
        //Notice that we do not throw the exception here, Java does.
        //also notice that I am requesting a pgm stack trace dump

        double total = 0;
        int    count = 0;

        try {
            total =  Double.parseDouble(args[0]);   //convert String to double
            count =  Integer.parseInt(args[1]);     //convert String to int 
        } 
        catch (Exception e) {
            System.out.println("Argument supplied is not a valid number \n" + e);
            e.printStackTrace();
            System.exit(0);             //request a stack trace dump
        }
                                        

        //---- Try calculating an average ------------------------------------------

        //in this try block, 
        //if the denominator == 0.  We raise a generic exception  
        //if the denominator  < 0.  We raise our own custom exception "NegativeException"
        //The order of which one is raised first is not important
        //The order of catching exception is VERY IMPORTANT
        //We must catch the more specific (exceptions first (subclasses)
        //BEFORE catching the generic Exception class

        double avg=0;

        try {
            if (count  < 0)
                throw new NegativeException( "count less than 0",  count);
            if (count == 0)
                throw new Exception("count equal to 0");
            else 
                avg = total/count;
        }
        catch (NegativeException e) {           //catch custom Exception
            System.out.println( e );
        }    
        catch (Exception e) {                   //catch more generic Exception
            System.out.println( e );
            e.printStackTrace();
        }
        return(avg);
    }
}