Effective Java - Exception

Exception's principle: Only have exceptions happened, use it, do not to use it control work flow, for three reasons: 1.when JVM optimize some common operation(e.g. for circle), when we use exception to instead it, JVM won't do optimize. 2. None of JVM used to optimize exception now. 3. Create and throw exception will take some JVM resources. 4. Hard to understand bussiness logic(my understanding). There is three kinds of Exception: Exception, RuntimeException, Error. Exception: Checked exception. RuntimeException: program error. Error: For environment error, JVM will handle it or define it. Error and RuntimeException 在表现形式上没有区别, 代表出错逻辑不同。 We should document the exception and don't catch or throw "Exception". Exception can be pass to high level throw transmit(转义), it means we can transmate one kind exception(JDK exception) to another kind of exception(Bussiness exception). If we want to store the low level exception, can pass low level exception information throw "Exception chain", before JDK1.4, we should implemnts it by ourselves, we define a Throw field in our custom exception, and store the low level exception as its part, After JDK1.4, we can pass low level exception throw Construct. Two way to instead of exception: Return a value, get a "testing method"(Iterator.hasnext()).如果有状态相关的方法, 最好有测试状态相关的代码。 User common exception like IllegalArgumentException, IllegalStateException, NullPointerException...

posted on 2006-09-04 09:00 Sheldon Sun 阅读(280) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

导航

统计

常用链接

留言簿(3)

随笔档案

文章档案

搜索

最新评论

阅读排行榜

评论排行榜