AN EXAMPLE OF EXCEPTION HANDLING

Please turn in your hypotheses before the start of lab.


In this lab you will see an example of how Java's exception-handling facilities enable you to avoid almost all abnormal terminations.

Constructor from HourlyEmployee

Here, from the HourlyEmployee class, is the constructor that takes a String parameter:

  /**
   *  Initializes this HourlyEmployee object’s name and gross pay from a
   *  a specified String object, which consists of a name, hours worked and
   *  pay rate, with at least one blank between each of those three 
   *  components.
   *
   *  @param s – the String object from which this HourlyEmployee object
   *                 is initialized.
   *
   *  @throws NoSuchElementException – if the hours worked or pay rate
   *                 is missing from s.                     
   *
   *  @throws NumberFormatException – if the hours worked or pay rate is
   *                  not a number.    
   *
   */ 
  public HourlyEmployee (String s) 
  {
    StringTokenizer tokens = new StringTokenizer (s);
    name = tokens.nextToken();
    hoursWorked = Integer.parseInt (tokens.nextToken());
    payRate = Double.parseDouble (tokens.nextToken());
    
    grossPay = hoursWorked * payRate;
  } // constructor with string parameter 

An exception would be thrown if the input line were: Jones 10.00 5.00