Effective Java 60 Favor the use of standard exceptions
2014-04-24 17:11 小郝(Kaibo Hao) 阅读(387) 评论(0) 编辑 收藏 举报Benefits to reuse preexisting exceptions
- It makes your API easier to learn and use.
- Programs using your API are easier to read.
- Fewer exception classes mean a smaller memory footprint and less time spent loading classes.
Common 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 |
Principle
- 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 63).
作者:小郝
出处:http://www.cnblogs.com/haokaibo/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://www.cnblogs.com/haokaibo/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。