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.

Exercises

13.15

List the various exceptional conditions that have occurred in programs throughout this text so far. List as many additional exceptional conditions as you can. For each of these, describe briefly how a program typically would handle the exception by using the exception-handling techniques discussed in this chapter. Some typical exceptions are division by zero, arithmetic overflow, array index out of bounds, etc.

 
13.16

Until this chapter, we have found that dealing with errors detected by constructors is a bit awkward. Explain why exception handling is an effective means for dealing with constructor failure.

13.17

(Catching Exceptions with Superclasses) Use inheritance to create an exception superclass (called ExceptionA) and exception subclasses ExceptionB and ExceptionC, where ExceptionB inherits from ExceptionA and ExceptionC inherits from ExceptionB. Write a program to demonstrate that the catch block for type ExceptionA catches exceptions of types ExceptionB and ExceptionC.

13.18

(Catching Exceptions Using Class Exception) Write a program that demonstrates how various exceptions are caught with

catch ( Exception exception )  

This time, define classes ExceptionA (which inherits from class Exception) and ExceptionB (which inherits from class ExceptionA). In your program, create try blocks that throw exceptions of types ExceptionA, ExceptionB, NullPointerException and IOException. All exceptions should be caught with catch blocks specifying type Exception.

13.19

(Order of catch Blocks) Write a program that shows that the order of catch blocks is important. If you try to catch a superclass exception type before a subclass type, the compiler should generate errors.

13.20

(Constructor Failure) Write a program that shows a constructor passing information about constructor failure to an exception handler. Define class SomeException, which throws an Exception in the constructor. You program should try to create an object of type SomeException, and catch the exception that is thrown from the constructor.

13.21

(Rethrowing Exceptions) Write a program that illustrates rethrowing an exception. Define methods someMethod and someMethod2. Method someMethod2 should initially throw an exception. Method someMethod should call someMethod2, catch the exception and rethrow it. Call someMethod from method main, and catch the rethrown exception. Print the stack trace of this exception.

13.22

(Catching Exceptions Using Outer Scopes) Write a program showing that a method with its own TRy block does not have to catch every possible error generated within the try. Some exceptions can slip through to, and be handled in, other scopes.

Категории