Catching multiple exceptions

public class HelloThere {

  public static void main(String[] args) {
  
    int repeat;
    
    try {
      repeat = Integer.parseInt(args[0]);
    }
    catch (ArrayIndexOutOfBoundsException e) {
      // pick a default value
      repeat = 1;
    }
    catch (NumberFormatException e) {
      // print an error message
      System.err.println("Usage: java HelloThere repeat_count" );
      System.err.println(
       "where repeat_count is the number of times to say Hello" );
      System.err.println("and given as an integer like 1 or 7" );
      return;
    }
    
    for (int i = 0; i < repeat; i++) {
      System.out.println("Hello");
    }
  
  }

}

Previous | Next | Top | Cafe au Lait

Copyright 1999 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified June 8, 1999