/**
 * example of throwing and catching Exceptions
 * in this case not all exceptions are caught by the double method.
 * So the double method must be declared as "throws Exception"
 * The main method catches that exception. 
 *
 * Example of entries that would cause exceptions:
 *      java Except2          --> Missing arguments 
 *      java Except2  AA   3  --> Argument not a number
 *      java Except2  10  -2  --> Cannot calculate average - count <= 0
 *
 * ** Also please note that this class instantiate itself
 */

public class Except2
{
    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);
        }


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

        double avg = 0;

        try {
            avg = pgm.calcAvg(args);             //call object's calcAvg method
        }
        catch (Exception e) {
            System.out.println("Cannot calculate average \n" + e);
        }

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

/**
 * Method to calculate average throws exception, some of which are not caught 
 * therefore the method must be declared as "throws Exception"
 *
 */
    double calcAvg(String[ ] args) throws Exception         //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.

        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");  //my message
            System.out.println(e);                                          //Java's message
            System.exit(0);
        }
                                        

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

        //in this try block, if the denominator is <= 0  
        //we throw an Exception("with a message")
        //But we do not catch it
        //We bubble this exception back to the caller
        //therefore, we must declare this method as "throws Exception"
    
        if (count <= 0)
            throw new Exception("count less than or equal to 0");
        else 
            return(total / count);
    }
}