Source of UsingExceptions.java


  1: // Fig. 13.7: UsingExceptions.java
  2: // Demonstrating getMessage and printStackTrace from class Exception.
  3: 
  4: public class UsingExceptions 
  5: {
  6:    public static void main( String args[] )
  7:    {
  8:       try 
  9:       { 
 10:          method1(); // call method1 
 11:       } // end try
 12:       catch ( Exception exception ) // catch exception thrown in method1
 13:       { 
 14:          System.err.printf( "%s\n\n", exception.getMessage() );
 15:          exception.printStackTrace(); // print exception stack trace
 16: 
 17:          // obtain the stack-trace information
 18:          StackTraceElement[] traceElements = exception.getStackTrace();
 19:          
 20:          System.out.println( "\nStack trace from getStackTrace:" );
 21:          System.out.println( "Class\t\tFile\t\t\tLine\tMethod" );
 22: 
 23:          // loop through traceElements to get exception description
 24:          for ( StackTraceElement element : traceElements ) 
 25:          {
 26:             System.out.printf( "%s\t", element.getClassName() );
 27:             System.out.printf( "%s\t", element.getFileName() );
 28:             System.out.printf( "%s\t", element.getLineNumber() );
 29:             System.out.printf( "%s\n", element.getMethodName() );
 30:          } // end for
 31:       } // end catch
 32:    } // end main
 33: 
 34:    // call method2; throw exceptions back to main
 35:    public static void method1() throws Exception
 36:    {
 37:       method2();
 38:    } // end method method1
 39: 
 40:    // call method3; throw exceptions back to method1
 41:    public static void method2() throws Exception
 42:    {
 43:       method3();
 44:    } // end method method2
 45: 
 46:    // throw Exception back to method2
 47:    public static void method3() throws Exception
 48:    {
 49:       throw new Exception( "Exception thrown in method3" );
 50:    } // end method method3
 51: } // end class UsingExceptions
 52: 
 53: /**************************************************************************
 54:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 55:  * Pearson Education, Inc. All Rights Reserved.                           *
 56:  *                                                                        *
 57:  * DISCLAIMER: The authors and publisher of this book have used their     *
 58:  * best efforts in preparing the book. These efforts include the          *
 59:  * development, research, and testing of the theories and programs        *
 60:  * to determine their effectiveness. The authors and publisher make       *
 61:  * no warranty of any kind, expressed or implied, with regard to these    *
 62:  * programs or to the documentation contained in these books. The authors *
 63:  * and publisher shall not be liable in any event for incidental or       *
 64:  * consequential damages in connection with, or arising out of, the       *
 65:  * furnishing, performance, or use of these programs.                     *
 66:  *************************************************************************/