it at multiple places in your program. With exceptions, you no longer need to check for errors
at the point of the method call, since the exception will guarantee that someone catches it.
You only need to handle the problem in one place, in the so-called exception handler. This
saves you code, and it separates the code that describes what you want to do during normal
execution from the code that is executed when things go awry. In general, reading, writing,
and debugging code becomes much clearer with exceptions than when using the old way of
error handling.
There are two basic models in exception-handling theory. Java supportst termination, 3 inwhich you assume that the error is so critical that there’s no way to get back to where the
exception occurred. Whoever threw the exception decided that there was no way to salvage the situation, and they don’t want to come back.The alternative is called resumption. It means that the exception handler is expected to dosomething to rectify the situation,and then the faulting method is retried, presuming success the second time. If you want resumption, it means you still hope to continue execution after the exception is handled.If you want resumption-like behavior in Java, don’t throw an exception when you encounter an error. Instead, call a method that fixes the problem. Alternatively, place your try block inside a while loop that keeps reentering the try block until the result is satisfactory.Historically, programmers using operating systems that supported resumptive exceptionhandling eventually ended up using termination-like code and skipping resumption. So although resumption sounds attractive at first, it isn’t quite so useful in practice. The dominant reason is probably the coupling that results: A resumptive handler would need to be aware of where the exception is thrown, and contain non-generic code specific to the throwing location. This makes the code difficult to write and maintain, especially for large systems where the exception can be generated from many points.
The Sequence is simply a fixed-sized array of Object with a class wrapped around it. Youcall add( ) to add a new Object to the end of the sequence (if there’s room left). To fetch each of the objects in a Sequence, there’s an interface called Selector. This is an example of the Iterator design pattern that you shall learn more about later in the book. A Selector allows you to see if you’re at the end( ), to access the current( ) Object, and to move to the next( ) Object in the Sequence. Because Selector is an interface, other classes can implement the interfaceintheir own ways, and other methods can take the interface as an argument, in order to create more general-purpose code. Here, the SequenceSelector is a private class that provides Selector functionality. In main( ), you can see the creation of a Sequence, followed by the addition of a number of String objects. Then, a Selector is produced with a call to selector( ), and this is used to move through the Sequence and select each item.At first, the creation of SequenceSelector looks like just another inner class. But examine it closely. Note that each of the methods—end( ), current( ), and next( )—refers to items, which is a reference that isn’t part of SequenceSelector, but is instead a private field in the enclosing class. However, the inner class can access methods and fields from the enclosing class as if it owned them. This turns out to be very convenient, as you can see in the preceding example.So an inner class has automatic access to the members of the enclosing class. How can this happen? The inner class secretly captures a reference to the particular object of the enclosing class that was responsible for creating it. Then, when you refer to a member of the enclosing class, that reference is used to select that member. Fortunately, the compiler takes care of all these details for you, but now you can see that an object of an inner class can be created only in association with an object of the enclosing class (when, as you shall see, the inner class is non-static). Construction of the inner-class object requires the reference to the object of the enclosing class, and the compiler will complain if it cannot access that reference. Most of the time this occurs without any intervention on the part of the programmer.
- 上一篇:应用程序的设计和开发英文文献和中文翻译
- 下一篇:MAP-REDUCE的程序和系统英文文献和中文翻译
-
-
-
-
-
-
-
C++最短路径算法研究和程序设计
中国传统元素在游戏角色...
巴金《激流三部曲》高觉新的悲剧命运
江苏省某高中学生体质现状的调查研究
g-C3N4光催化剂的制备和光催化性能研究
现代简约美式风格在室内家装中的运用
NFC协议物理层的软件实现+文献综述
浅析中国古代宗法制度
高警觉工作人群的元情绪...
上市公司股权结构对经营绩效的影响研究