Throwing Exceptions

The throw keyword

public class Clock {

  private int hours;   // 1-12
  private int minutes; // 0-59
  private int seconds; // 0-59

  public Clock(int hours, int minutes, int seconds) {

    if (hours < 1 || hours > 12) {
      throw new IllegalArgumentException("Hours must be between 1 and 12");
    }
    if (minutes < 0 || minutes > 59) {
      throw new IllegalArgumentException("Minutes must be between 0 and 59");
    }
    if (seconds < 0 || seconds > 59) {
      throw new IllegalArgumentException("Seconds must be between 0 and 59");
    }
  
    this.hours   = hours;
    this.minutes = minutes;
    this.seconds = seconds;
  
  }
  
  public Clock(int hours, minutes) {
    this(hours, minutes, 0);
  }

  public Clock(int hours) {
    this(hours, 0, 0);
  }


}

Previous | Next | Top | Cafe au Lait

Copyright 1998-2000, 2002, 2003 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified March 5, 2003