Sometimes a catch block catches one exception type, then throws a new exception of a different type to indicate that a program-specific exception occurred. In earlier Java versions, there was no mechanism to wrap the original exception information with the new exception's information to provide a complete stack trace showing where the original problem occurred in the program. This made debugging such problems particularly difficult. J2SE 1.4 added chained exceptions to enable an exception object to maintain the complete stack-trace information. Figure 13.8 presents a mechanical example that demonstrates how chained exceptions work.
Figure 13.8. Chained exceptions.
(This item is displayed on pages 661 - 662 in the print version)
1 // Fig. 13.8: UsingChainedExceptions.java 2 // Demonstrating chained exceptions. 3 4 public class UsingChainedExceptions 5 { 6 public static void main( String args[] ) 7 { 8 try 9 { 10 method1(); // call method1 11 } // end try 12 catch ( Exception exception ) // exceptions thrown from method1 13 { 14 exception.printStackTrace(); 15 } // end catch 16 } // end main 17 18 // call method2; throw exceptions back to main 19 public static void method1() throws Exception 20 { 21 try 22 { 23 method2(); // call method2 24 } // end try 25 catch ( Exception exception ) // exception thrown from method2 26 { 27 throw new Exception( "Exception thrown in method1", exception ); 28 } // end try 29 } // end method method1 30 31 // call method3; throw exceptions back to method1 32 public static void method2() throws Exception 33 { 34 try 35 { 36 method3(); // call method3 37 } // end try 38 catch ( Exception exception ) // exception thrown from method3 39 { 40 throw new Exception( "Exception thrown in method2", exception ); 41 } // end catch 42 } // end method method2 43 44 // throw Exception back to method2 45 public static void method3() throws Exception 46 { 47 throw new Exception( "Exception thrown in method3" ); 48 } // end method method3 49 } // end class UsingChainedExceptions
|
The program consists of four methodsmain (lines 616), method1 (lines 1929), method2 (lines 3242) and method3 (lines 4548). Line 10 in method main's TRy block calls method1. Line 23 in method1's try block calls method2. Line 36 in method2's TRy block calls method3. In method3, line 47 throws a new Exception. Because this statement is not in a TRy block, method3 terminates, and the exception is returned to the calling method (method2) at line 36. This statement is in a TRy block; therefore, the try block terminates and the exception is caught at lines 3841. Line 40 in the catch block throws a new exception. In this case, the Exception constructor with two arguments is called. The second argument represents the exception that was the original cause of the problem. In this program, that exception occurred at line 47. Because an exception is thrown from the catch block, method2 terminates and returns the new exception to the calling method (method1) at line 23. Once again, this statement is in a try block, so the try block terminates and the exception is caught at lines 2528. Line 27 in the catch block throws a new exception and uses the exception that was caught as the second argument to the Exception constructor. Because an exception is thrown from the catch block, method1 terminates and returns the new exception to the calling method (main) at line 10. The try block in main terminates, and the exception is caught at lines 1215. Line 14 prints a stack trace.
Notice in the program output that the first three lines show the most recent exception that was thrown (i.e., the one from method1 at line 23). The next four lines indicate the exception that was thrown from method2 at line 40. Finally, the last four lines represent the exception that was thrown from method3 at line 47. Also notice that, as you read the output in reverse, it shows how many more chained exceptions remain.
Introduction to Computers, the Internet and the World Wide Web
Introduction to Java Applications
Introduction to Classes and Objects
Control Statements: Part I
Control Statements: Part 2
Methods: A Deeper Look
Arrays
Classes and Objects: A Deeper Look
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
GUI Components: Part 1
Graphics and Java 2D™
Exception Handling
Files and Streams
Recursion
Searching and Sorting
Data Structures
Generics
Collections
Introduction to Java Applets
Multimedia: Applets and Applications
GUI Components: Part 2
Multithreading
Networking
Accessing Databases with JDBC
Servlets
JavaServer Pages (JSP)
Formatted Output
Strings, Characters and Regular Expressions
Appendix A. Operator Precedence Chart
Appendix B. ASCII Character Set
Appendix C. Keywords and Reserved Words
Appendix D. Primitive Types
Appendix E. (On CD) Number Systems
Appendix F. (On CD) Unicode®
Appendix G. Using the Java API Documentation
Appendix H. (On CD) Creating Documentation with javadoc
Appendix I. (On CD) Bit Manipulation
Appendix J. (On CD) ATM Case Study Code
Appendix K. (On CD) Labeled break and continue Statements
Appendix L. (On CD) UML 2: Additional Diagram Types
Appendix M. (On CD) Design Patterns
Appendix N. Using the Debugger
Inside Back Cover