1 /**
    2  * Cone demonstrates throwing runtime exceptions and the
    3  * try-finally construct.  Download Cone
    4  *
    5  * @author Will Benton <willb@cs.wisc.edu>
    6  * @version $Rev$
    7  */
    8 public final class Cone {
    9     private int scoops;
   10 
   11     public Cone(int scoops) { this.scoops = scoops; }
   12 
   13     /**
   14     Eats x scoops from this cone.  PRECONDITION:  x >= 0
   15     */
   16     public void eat(int x) {
   17         if (x < 0) {
   18             throw new RuntimeException(x + " is an invalid number of scoops.");
   19         }
   20         scoops -= x;
   21     }
   22     
   23     public static void main(String[] args) {
   24         Cone c = new Cone(5);
   25         try {
   26             /* What happens if this doesn't throw an exception? */
   27             c.eat(-4);
   28         } finally {
   29             System.err.println("All OK.");
   30         }
   31         
   32         System.out.println("all done!");
   33         
   34     }
   35 }
   36