Source of ExceptionDemo.java


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