Source of ExceptionDemo.java


  1: 
  2: import java.util.*;
  3: 
  4: public class ExceptionDemo
  5: {
  6:     public static void main(String[] args)
  7:     {
  8:         int donutCount, milkCount;
  9:         double donutsPerGlass;
 10:         Scanner keyboard = new Scanner(System.in);
 11: 
 12:         try
 13:         {
 14:             System.out.println("Enter number of donuts:");
 15:             donutCount = keyboard.nextInt( );
 16:             System.out.println("Enter number of glasses of milk:");
 17:             milkCount = keyboard.nextInt( );
 18:             if (milkCount < 1)
 19:                throw new Exception("Exception: No Milk!");
 20:             donutsPerGlass = donutCount/(double)milkCount;
 21:             System.out.println(donutCount + " donuts.");
 22:             System.out.println(milkCount + " glasses of milk.");
 23:             System.out.println("You have " + donutsPerGlass
 24:                            + " donuts for each glass of milk.");
 25:         }
 26:         catch(Exception e)
 27:         {
 28:             System.out.println(e.getMessage( ));
 29:             System.out.println("Go buy some milk.");
 30:         }
 31:         System.out.println("End of program.");
 32:     }
 33: }