
import java.util.*;

public class TwoCatchesDemo
{
   public static void main(String[] args)
   {
      try
      {
         int widgets, defective;
         double ratio;

         System.out.println("Enter number of widgets produced:");
         Scanner keyboard = new Scanner(System.in);
         widgets = keyboard.nextInt( );
         if (widgets < 0)
             throw new NegativeNumberException("widgets");

         System.out.println("How many were defective?");
         defective = keyboard.nextInt( );
         if (defective < 0)
             throw new NegativeNumberException("defective widgets");

         ratio = exceptionalDivision(widgets, defective);
         System.out.println( "One in every  " + ratio
                                    + " widgets is defective.");
      }
      catch(DivideByZeroException e)
      {
         System.out.println("Congratulations! A perfect record!");
      }
      catch(NegativeNumberException e)
      {
         System.out.println("Cannot have a negative number of "
                             + e.getMessage( ));
      }
      System.out.println("End of program.");
   }

   public static double exceptionalDivision(double numerator,
                   double denominator) throws DivideByZeroException

   {
       if (denominator == 0)
             throw new DivideByZeroException( );
       return (numerator/denominator);
   }
}