Wrap-Up
Answers to Self Review Exercises
13.1 |
Memory exhaustion, array index out of bounds, arithmetic overflow, division by zero, invalid method parameters. |
13.2 |
(a) Exception handling is designed to handle infrequently occurring situations that often result in program termination, not situations that arise all the time. (b) Flow of control with conventional control structures is generally clearer and more efficient than with exceptions. (c) The "additional" exceptions can get in the way of genuine error-type exceptions. It becomes more difficult for the programmer to keep track of the larger number of exception cases. |
13.3 |
It is unlikely that methods of classes in the Java API could perform error processing that would meet the unique needs of all users. |
13.4 |
A "resource leak" occurs when an executing program does not properly release a resource when it is no longer needed. |
13.5 |
The catch blocks for that try statement are skipped, and the program resumes execution after the last catch block. If there is a finally block, it is executed first; then the program resumes execution after the finally block. |
13.6 |
The form catch( Exception exceptionName ) catches any type of exception thrown in a try block. An advantage is that no thrown Exception can slip by without being caught. The programmer can then decide to handle the exception or possibly rethrow it. |
13.7 |
Errors are usually serious problems with the underlying Java system; most programs will not want to catch Errors because the program will not be able to recover from such problems. |
13.8 |
This causes the search for a match to continue in the next enclosing try statement. If there is a finally block, it will be executed before the exception goes to the next enclosing TRy statement. If there are no enclosing try statements for which there are matching catch blocks, and the exception is checked, a compilation error occurs. If there are no enclosing try statements for which there are matching catch blocks and the exception is unchecked, a stack trace is printed and the current thread terminates early. |
13.9 |
The first matching catch block after the try block is executed. |
13.10 |
This enables a program to catch related types of exceptions and process them in a uniform manner. However, it is often useful to process the subclass types individually for more precise exception handling. |
13.11 |
The finally block is the preferred means for releasing resources to prevent resource leaks. |
13.12 |
First, control passes to the finally block if there is one. Then the exception will be processed by a catch block (if one exists) associated with an enclosing try block (if one exists). |
13.13 |
It rethrows the exception for processing by an exception handler of an enclosing try statement, after the finally block of the current try statement executes. |
13.14 |
The reference goes out of scope, and the reference count for the object is decremented. If the reference count becomes zero, the object is marked for garbage collection. |