// ------------------------------------------------------------------
// Exceptions.java -- Console demo on exception handling in Java
//
//   The class Exceptions has a main() method which calls another
//   static method, doSomething(). This test method expects a 
//   nonnegative integer as its only parameter. If this integer is 
//   negative, doSomething() gets upset enough to throw an exception.
//   To show how to make your own subclass of java.lang.Exception,
//   this test defines a new class MyException as the type of
//   exception that doSomething() throws.
//
//   The main() method handles this exception by enclosing the call
//   to doSomething() in a "try" block, which is provided with a
//   "catch" block for MyException.
//
// File Revision Date: 08/21/08
// ------------------------------------------------------------------

public class Exceptions {

  public static void main (String [] args) {
    try {
      System.out.println ();
      System.out.println ("Enter try block...");

      doSomething (1);

      System.out.println ("Got past first call...");

      doSomething (-1);

      // You should not see any of the following messages
      // unless you edit the line above to replace -1 with
      // a nonnegative integer...

      System.out.println ("Got past second call...");
      System.out.println ("Exit try block...");
    }
    catch (MyException e) {
      System.out.println (
        "Caught MyException object, getMessage() = " + e.getMessage());
    }

    catch (Exception e) {
      System.out.println (
        "Caught Exception object, getMessage() = " + e.getMessage());
    }
    finally {     // Optional wrapup block... executes no matter what...
      System.out.println ("That's all, folks!");
    }
  }

  public static void doSomething (int n) throws MyException {
    if (n < 0)
      throw new MyException ();
  }
}

class MyException extends Exception {
  public String getMessage () {
    return "I take exception!!!";
  }
}
