自测题还是挺平易近人的,都是一些很基本的问题。
Self-Review Exercises
11.1 List five common examplesof exceptions.
(Memory exhaustion, array index out of bounds, arithmeticoverflow, division by zero, invalid method parameters.)
11.2 Why are exceptionsparticularly appropriate for dealing with errors produced by methods of classesin the Java API?
(It’s unlikely that methods of classesin the Java API could perform error processing that would meet the unique needsof all users.)
11.3 What is a “resource leak”?
(A “resource leak”occurs when an executing program does not properly release a resource when it’s no longer needed.)
11.4 If no exceptions arethrown in a try block, where does control proceed to when the try block completes execution?
(The catch blocks for that trystatement are skipped, and the program resumesexecution after the last catchblock. If there’s a finally block, it’s executed first; then theprogram resumes execution after the finally block.)
11.5 Give a key advantage ofusing catch(Exception exceptionName).
(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. You can handle the exceptionor rethrow it.)
11.6 Should a conventionalapplication catch Error objects? Explain.
(Errors are usually serious problems with the underlying Java system;most programs will not want to catch Errors because they will notbe able to recover from them.)
11.7 What happens if no catch handler matches the type of a thrown object?
(This causes the searchfor a match to continue in the next enclosing try statement. If there’s a finally block, it will be executed before the exception goes to the nextenclosing try statement.
If there are no enclosing try statements for whichthere are matching catch blocks and the exceptions are declared (or unchecked), a stacktrace is printed and the current thread terminates early. If the exceptions arechecked, but not caught or declared, compilation errors occur.)
11.8 What happens if several catch blocks match the type of the thrown object?
(The first matching catch block after the tryblock is executed.)
11.9 Why would a programmerspecify a superclass type as the type in a catch block?
(This enables a program tocatch related types of exceptions and process them in a uniform manner.However, it’s often useful to process the subclasstypes individually for more precise exception handling.)
11.10 What is the key reasonfor using finally blocks?
(The finally block is the preferred means for releasing resources to preventresource leaks.)
11.11 What happens when a catch block throws an Exception?
(First, control passes tothe finally block if there is one. Then the exception will be processed by acatch block (if one exists) associated with an enclosing try block (if one exists).)
11.12 What does the statement throw exceptionReference do in a catch block?
(It rethrows the exceptionfor processing by an exception handler of an enclosing try statement, after the finally block of the current try statement executes.)
11.13 What happens to a localreference in a tryblock when that blockthrows an Exception?
(The reference goes out ofscope. If the referenced object becomes unreachable, the object can be garbagecollected.)