1. Reusing preexisting exceptions has several benefits. Chief among these, it
makes your API easier to learn and use because it matches established conventions with which programmers are already familiar. A close second is that programs using your API are easier to read because they aren’t cluttered with unfamiliar exceptions. Last (and least), fewer exception classes mean a smaller memory footprint and less time spent loading classes.
2. The most commonly reused exception is IllegalArgumentException. This is generally the exception to throw when the caller passes in an argument whose value is inappropriate. Another commonly reused exception is IllegalStateException. This is generally the exception to throw if the invocation is illegal because of the state of the receiving object. Arguably, all erroneous method invocations boil down to an illegal argument or illegal state, but other exceptions are standardly used for certain kinds of illegal arguments and states.
3. If a caller passes null in some parameter for which null values are prohibited, convention dictates that NullPointerException be thrown rather than IllegalArgumentException.
4. If a caller passes an out-of range value in a parameter representing an index into a sequence, IndexOutOfBoundsException should be thrown rather than IllegalArgumentException.
5. ConcurrentModificationException should be thrown if an object that was designed for use by a single thread or with external synchronization detects that it is being (or has been) concurrently modified.
6. UnsupportedOperationException is the exception to throw if an object does not support an attempted operation. This exception is used by implementations that fail to implement one or more optional operations defined by an interface.
7. The following table summarizes the most commonly reused exceptions:
Exception | Occasion for Use |
IllegalArgumentException | Non-null parameter value is inappropriate |
IllegalStateException | Object state is inappropriate for method invocation |
NullPointerException | Parameter value is null where prohibited |
IndexOutOfBoundsException | Index parameter value is out of range |
ConcurrentModificationException |
Concurrent modification of an object has been detected where it is prohibited |
UnsupportedOperationException |
Object does not support method |
8. It would be appropriate to reuse ArithmeticException and NumberFormatException if you were implementing arithmetic objects such as complex numbers or rational numbers.
9. Reuse must be based on semantics, not just on name. Also, feel free to subclass an existing exception if you want to add a bit more failure-capture information.
相关推荐
Item 72: Favor the use of standard exceptions Item 73: Throw exceptions appropriate to the abstraction Item 74: Document all exceptions thrown by each method Item 75: Include failure-capture ...
【英文】德银报告:中国电子商务报告China E-commerce:A tale of two camps - and we favor the(72页),资源名称:【英文】德银报告:中国电子商务报告China E-commerce:A tale of two camps - and we favor the...
The bailout was a reward for failure, in favor of the privileged and advantaged. It was the taxpayers and disadvantaged ones paying the price and assuming the risk. This is the perfect time for ...
Favor A easy way of using Android SharedPreferences....2 The FavorAdapter class generates an implementation of the interface. account = new FavorAdapter.Builder(getContext()).build().create(Account.cla
All of a project's header files should be listed as descentants of the project's source directory without use of UNIX directory shortcuts . (the current directory) or .. (the parent directory). For...
In general, Ruby programmers tend to favor the the command line for running their code, and they tend to use text editors rather than IDEs. * Ruby runs on Windows, Linux, and Macs.
表演者 + 注意力机制的简单 Numpy 实现在Rethinking Attention with Performers ,以及在我的文章中复制实验的代码: 。
Use the Q Experience Composer, the first-of-its-kind web console to create visual programs/experiments and submit them to a quantum simulator or real device on the cloud Run programs remotely using ...
【英文】德银报告:中国电子商务报告China E-commerce:A tale of two camps - and we favor the
have happened years ago: the deprecation of the JSF managed bean facility in favor of using CDI beans. Other features are support for WebSocket using the Java EE WebSocket APIs donated by OmniFaces,...
In this version I have done away with the b+tree and hash index in favor of my own MGIndex structure which for all intents and purposes is superior and the performance numbers speak for themselves....
Microsoft did have a similar platform internally called Dryad but, shortly before Dryad was expected to go live, it was dropped in favor of creating a distribution of Hadoop in conjunction with ...
With the development of network ,Everyone's life become more and more fast-paced,We use the computer more and more, People face-to-face exchanges between the time to become less and less,Makes the ...
happens during the course of the true developmental phases of the new product development. This situation creates a need for tapping into the skills and abilities of all the participants in the ...
The C code examples used in the book are traditional in that they favor the UNIX environment. However, all is not lost. If you visit the publishers website you will find WinSock adaptations for ...
46 PROC. OF THE 12th PYTHON IN SCIENCE CONF. (SCIPY 2013)Pythran: Enabling Static Optimization of Scientific ...written in a subset of Python into native ones. Based on the fact that...them in favor of powe
driven continuous improvement, the collection, analysis, and application of meaningful metrics often fades in favor of subjective measures that offer less insight into the real challenges of making ...
The Metro app user experience is very different from previous generations of Windows applications: Metro apps are full-screen and favor a usability style that is simple, direct, and free from ...
adaptively to favor those making the best predictions. The output of two independent mixers (which use sets of weights selected by different contexts) are averaged. This estimate is then adjusted by ...
To make use of either more or less strict isolation levels in applications, locking can be customized for an entire session by setting the isolation level of the session with the SET TRANSACTION ...