JHTP自测题_第十一章_深入理解异常(Exception Handling)

自测题还是挺平易近人的,都是一些很基本的问题。


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?

(Its 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 leakoccurs when an executing program does not properly release a resource when its 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 theres a finally block, its 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 theres 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, its 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.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值