/** Java has an exception mechanism similar to that of ML.  Exceptions are
    objects extending the class Exception.  Computation may be aborted by
    throwing an exception, which may be caught by a dynamically enclosing
    handler.  Exception handlers may exploit the class hierarchy to
    selectively handle classes of exceptions.

    Methods that may raise an uncaught exception are required to state this
    with a "throws" clause.  Run-time errors are not required to be explicitly
    indicated in this manner.
*/

class RecipException extends Exception {

  public String msg;

  RecipException (String msg) {
    super (msg);
    this.msg = msg;
  }

}

class ExceptionDemo {

  public static float recip (float x) throws RecipException {

    if (x == 0.0) {
      throw new RecipException ("Illegal argument");
    } else {
      return ((float)(1.0 / x));
    }

  }

  public static void main (String argv[]) {

    float arg = (float)0.0;

    if (argv.length > 0) {
      arg = Float.valueOf(argv[0]).floatValue();
    }

    String msg = "Can't happen.";

    try { msg = Float.toString(recip(arg));}
    catch (RecipException x) { msg = x.msg; };

    System.out.println("Result = " + msg);

  }

}
