Source of GetMilk.java


  1: /**
  2:    Demonstrates the behavior of a finally block.
  3:    @author Frank M. Carrano
  4:    @author Timothy M. Henry
  5:    @version 5.0
  6: */
  7: public class GetMilk
  8: {
  9:    public static void main(String[] args)
 10:    {
 11:       try
 12:       {
 13:          openRefrigerator();
 14:          takeOutMilk();
 15:          pourMilk();
 16:          putBackMilk();
 17:       }
 18:       catch (NoMilkException e)
 19:       {
 20:          System.out.println(e.getMessage());
 21:       }
 22:       finally
 23:       {
 24:          closeRefrigerator();
 25:       }
 26:    } // end main
 27:    
 28:    public static void openRefrigerator()
 29:    {
 30:       System.out.println("Open the refrigerator door.");
 31:    } // end openRefrigerator
 32:    
 33:    public static void takeOutMilk() throws NoMilkException
 34:    {
 35:       if (Math.random() < 0.5)
 36:          System.out.println("Take out the milk.");
 37:       else
 38:          throw new NoMilkException("Out of Milk!");
 39:    } // end openRefrigerator
 40:    
 41:    // < The methods pourMilk, putBackMilk, and closeRefrigerator are analogous to
 42:    //   openRefrigerator and are here. >
 43:    // . . .
 44: } // end GetMilk