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...
描述中提到的“FAVOR+注意力机制”是Performer模型的一个变体,FAVOR代表“Fast Attention Via Orthogonal Random Features”,它利用正交随机特征来近似自注意力,从而实现在Numpy环境下的高效计算。这种方法的关键...
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 ...
(the majority of):The majority of students are in favor of the decision that the school made. - 暑假我们去了西安,那里有很多名胜古迹。(where 引导非限制性定语从句):During the summer holiday we went ...
例如:“As far as I am concerned, I am in favor of having an American teacher, which is sure to do more good than harm.” 这样的表述既表明了立场,也给出了解释。 通过以上知识点的学习,学生可以更有效地...
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....
- **例句:**After years of ruling, the king decided to abdicate in favor of his son. 6. **Abdomen n. In mammals, the visceral cavity between the diaphragm and the pelvic floor; the belly.** - **...
15. 利用:make full use of, make the most (best) of, take advantage of。表示有效利用资源。 16. 显露:show up, turn up, appear。表示出现或现身。 17. 值得:be worth doing, be worthy of being done, be ...
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 ...
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 ...
- 支持某一方:in favor of;on the side of —— 显示对某一立场或观点的支持。 6. 其他表达: - 不会犯错的:infallible —— 描述绝对正确或无误的事物。 - 不恰当:inappropriate;improper;unsuitable;...
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 ...