// file:     TestThrow.java
// author:   Robert Keller
// purpose:  demonstrating user-defined exceptions, catch and throw

import java.io.StreamTokenizer;
import java.io.*;

// programmer defined exception

class numException extends Exception
{
String value;

numException(String value)
  {
  this.value = value;
  }

public String toString()
  {
  return "numException, value: " + value;
  }
}


class TestThrow
{

public static void main(String arg[])
  {
  StreamTokenizer in = new StreamTokenizer(System.in);
  in.parseNumbers();
  try
    {
    System.out.println("Enter numerals.");  
    System.out.print("If you dare enter something ");
    System.out.println("non-numeric, an exception will be thrown.");
    System.out.println();
    System.out.println("Note: By its design, the StreamTokenizer class used ");
    System.out.println("here does not recognize exponents, as in 1.2e34.");
    System.out.println();
outer:
    while( true )
      {
      try
        { 
        switch( in.nextToken() )                              // read input
          {
          case StreamTokenizer.TT_EOF:
            break outer;                                      // normal exit

          case StreamTokenizer.TT_NUMBER:
            break;                                            // numeric token

          default:
            throw new numException("You entered " + in.sval); // abnormal exit
          }
        }
      catch( IOException e )                    // numException not caught here
        {
        throw e;                                // re-throw existing exception
        }
      }
    }
  catch( numException e )
    {
    System.out.println("*** Caught " + e);
    System.exit(1);
    }
  catch( IOException e )
    {
    System.out.println("*** Caught IOException");
    System.exit(1);
    }
  System.out.println("normal exit");
  }
}

