Commonly reused unchecked exceptions provided by Java platform libraries - 复用这些大家都知道的exceptions,好处就是大家都熟悉,容易理解,也知道这些异常所代表的用途。
The most commonly reused exceptions
Exceptions | Occasion for Use | Example |
---|---|---|
IllegalArgumentException | Non-null parameter value is inappropriate | pass a negative number in a parameter representing the number of times some action to be repeated |
IllegalStateException | Object state is inappropriate for method invocation | attempt to use some object before it is properly initialized |
NullPointException | 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 | an object that is designed by a single thread or with external synchronization detects that it is being concurrently modified |
UnsupportedOperationException | Object does not support method | an append List implementation would throw this if someone tries to delete an element from the list |
如果还有其它的exceptions符合你的需要,在符合exception java documentation的情况下,按照他的semantics意义来使用这些exception,而不是根据exception的名字。如果你要加一些failure-capture的信息,你也可以subclass这些异常。
最后选择异常,不是一个硬核科学,没有一个准数。某些情况,你可以发现有好几个上面举例的异常都能匹配,挑选一个比较合适的既可。