Source of ExceptionDemo.java


  1: //ExceptionDemo.java
  2: 
  3: import java.util.Scanner;
  4: 
  5: public class ExceptionDemo
  6: {
  7:     public static void main(String[] args)
  8:     {
  9:         Scanner keyboard = new Scanner(System.in);
 10: 
 11:         try
 12:         {
 13:             System.out.print("Enter number of donuts: ");
 14:             int donutCount = keyboard.nextInt( );
 15:             System.out.print("Enter number of glasses of milk: ");
 16:             int milkCount = keyboard.nextInt( );
 17:             if (milkCount < 1) 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: }
 32: