Source of UsingExceptions.java


  1: // Fig. 13.6: UsingExceptions.java
  2: // Demonstration of stack unwinding.
  3: 
  4: public class UsingExceptions 
  5: {
  6:    public static void main( String args[] )
  7:    {
  8:       try // call throwException to demonstrate stack unwinding
  9:       { 
 10:          throwException();
 11:       } // end try
 12:       catch ( Exception exception ) // exception thrown in throwException
 13:       {
 14:          System.err.println( "Exception handled in main" );
 15:       } // end catch
 16:    } // end main
 17: 
 18:    // throwException throws exception that is not caught in this method
 19:    public static void throwException() throws Exception
 20:    {
 21:       try // throw an exception and catch it in main
 22:       { 
 23:          System.out.println( "Method throwException" );
 24:          throw new Exception(); // generate exception
 25:       } // end try
 26:       catch ( RuntimeException runtimeException ) // catch incorrect type
 27:       {
 28:          System.err.println( 
 29:             "Exception handled in method throwException" );
 30:       } // end catch
 31:       finally // finally block always executes
 32:       { 
 33:          System.err.println( "Finally is always executed" );
 34:       } // end finally
 35:    } // end method throwException
 36: } // end class UsingExceptions
 37: 
 38: /**************************************************************************
 39:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 40:  * Pearson Education, Inc. All Rights Reserved.                           *
 41:  *                                                                        *
 42:  * DISCLAIMER: The authors and publisher of this book have used their     *
 43:  * best efforts in preparing the book. These efforts include the          *
 44:  * development, research, and testing of the theories and programs        *
 45:  * to determine their effectiveness. The authors and publisher make       *
 46:  * no warranty of any kind, expressed or implied, with regard to these    *
 47:  * programs or to the documentation contained in these books. The authors *
 48:  * and publisher shall not be liable in any event for incidental or       *
 49:  * consequential damages in connection with, or arising out of, the       *
 50:  * furnishing, performance, or use of these programs.                     *
 51:  *************************************************************************/