Source of UsingExceptions.java


  1: // Fig. 13.5: UsingExceptions.java
  2: // Demonstration of the try...catch...finally exception handling
  3: // mechanism.
  4: 
  5: public class UsingExceptions 
  6: {
  7:    public static void main( String args[] )
  8:    {
  9:       try 
 10:       { 
 11:          throwException(); // call method throwException
 12:       } // end try
 13:       catch ( Exception exception ) // exception thrown by throwException
 14:       {
 15:          System.err.println( "Exception handled in main" );
 16:       } // end catch
 17: 
 18:       doesNotThrowException();
 19:    } // end main
 20: 
 21:    // demonstrate try...catch...finally
 22:    public static void throwException() throws Exception
 23:    {
 24:       try // throw an exception and immediately catch it
 25:       { 
 26:          System.out.println( "Method throwException" );
 27:          throw new Exception(); // generate exception
 28:       } // end try
 29:       catch ( Exception exception ) // catch exception thrown in try
 30:       {
 31:          System.err.println(
 32:             "Exception handled in method throwException" );
 33:          throw exception; // rethrow for further processing
 34: 
 35:          // any code here would not be reached, exception rethrown in catch
 36: 
 37:       } // end catch
 38:       finally // executes regardless of what occurs in try...catch
 39:       {
 40:          System.err.println( "Finally executed in throwException" );
 41:       } // end finally
 42: 
 43:       // any code here would not be reached
 44: 
 45:    } // end method throwException
 46: 
 47:    // demonstrate finally when no exception occurs
 48:    public static void doesNotThrowException()
 49:    {
 50:       try // try block does not throw an exception
 51:       { 
 52:          System.out.println( "Method doesNotThrowException" );
 53:       } // end try
 54:       catch ( Exception exception ) // does not execute
 55:       {
 56:          System.err.println( exception );
 57:       } // end catch
 58:       finally // executes regardless of what occurs in try...catch
 59:       {
 60:          System.err.println( 
 61:             "Finally executed in doesNotThrowException" );
 62:       } // end finally
 63: 
 64:       System.out.println( "End of method doesNotThrowException" );
 65:    } // end method doesNotThrowException
 66: } // end class UsingExceptions
 67: 
 68: /**************************************************************************
 69:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 70:  * Pearson Education, Inc. All Rights Reserved.                           *
 71:  *                                                                        *
 72:  * DISCLAIMER: The authors and publisher of this book have used their     *
 73:  * best efforts in preparing the book. These efforts include the          *
 74:  * development, research, and testing of the theories and programs        *
 75:  * to determine their effectiveness. The authors and publisher make       *
 76:  * no warranty of any kind, expressed or implied, with regard to these    *
 77:  * programs or to the documentation contained in these books. The authors *
 78:  * and publisher shall not be liable in any event for incidental or       *
 79:  * consequential damages in connection with, or arising out of, the       *
 80:  * furnishing, performance, or use of these programs.                     *
 81:  *************************************************************************/