When an exception is thrown but not caught in a particular scope, the method-call stack is "unwound," and an attempt is made to catch the exception in the next outer try block. This process is called stack unwinding. Unwinding the method-call stack means that the method in which the exception was not caught terminates, all local variables in that method go out of scope and control returns to the statement that originally invoked that method. If a TRy block encloses that statement, an attempt is made to catch the exception. If a try block does not enclose that statement, stack unwinding occurs again. If no catch block ever catches this exception and the exception is checked (as in the following example), compiling the program will result in an error. The program of Fig. 13.6 demonstrates stack unwinding.
Figure 13.6. Stack unwinding.
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
|
When method main executes, line 10 in the TRy block calls method throwException (lines 1935). In the try block of method throwException (lines 2125), line 24 throws an Exception. This terminates the try block immediately, and control skips the catch block at line 26, because the type being caught (RuntimeException) is not an exact match with the thrown type (Exception) and is not a superclass of it. Method throwException terminates (but not until its finally block executes) and returns control to line 10the point from which it was called in the program. Line 10 is in an enclosing try block. The exception has not yet been handled, so the try block terminates and an attempt is made to catch the exception at line 12. The type being caught (Exception) does match the thrown type. Consequently, the catch block processes the exception, and the program terminates at the end of main. If there were no matching catch blocks, a compilation error would occur. Remember that this is not always the casefor unchecked exceptions, the application will compile, but run with unexpected results.
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