Throwing Exceptions

 

The throw keyword
public class Clock {

  int hours;   // 1-12
  int minutes; // 0-59
  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);
  }


}

List | Previous | Next