注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

淡泊明智

 
 
 

日志

 
 

从 FingBugs的错误来看JAVA代码质量(五)  

2012-11-19 10:34:44|  分类: 代码质量 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

博客分类:
JAVA代码质量


REC_CATCH_EXCEPTION
从 FingBugs的错误来看JAVA代码质量(五) - 火木棉 - 淡泊明智
Bug: Exception is caught when Exception is not thrown
Pattern id: REC_CATCH_EXCEPTION, type: REC, category: STYLE
This method uses a try-catch block that catches Exception objects, but Exception is not thrown within the try block, and RuntimeException is not explicitly caught. It is a common bug pattern to say try { ... } catch (Exception e) { something } as a shorthand for catching a number of types of exception each of whose catch blocks is identical, but this construct also accidentally catches RuntimeException as well, masking potential bugs.
这想写会无意中把RuntimeException也捕获了,有可能导致潜在的bug。 JVM对RuntimeException有统一的捕获机制,让JVM来处理它。
在try/catch块中捕获异常,但是异常没有在try语句中抛出而RuntimeException又没有明确的被捕获
1.比较推荐的写法一般如下(即指明具体的异常类型)
Java代码 复制代码 收藏代码从 FingBugs的错误来看JAVA代码质量(五) - 火木棉 - 淡泊明智
try {   
}catch(IOException e){   
  
}finally{   
}  


2.捕获了异常,一定要处理异常
还有人在catch里面什么都不写,就写上
3.避免在大的语句块里面写try,catch,因为本身也比较耗费时间,而且不便于调试和发现问题。
Bug: Call to equals() comparing different types
Pattern id: EC_UNRELATED_TYPES, type: EC, category: CORRECTNESS
从 FingBugs的错误来看JAVA代码质量(五) - 火木棉 - 淡泊明智
This method calls equals(Object) on two references of different class types with no common subclasses. Therefore, the objects being compared are unlikely to be members of the same class at runtime (unless some application classes were not analyzed, or dynamic class loading can occur at runtime). According to the contract of equals(), objects of different classes should always compare as unequal; therefore, according to the contract defined by java.lang.Object.equals(Object), the result of this comparison will always be false at runtime.
两个不同类型的对象调用equals,将永远返回false,除非你重写了equals方法。
调用equals方法比较不同类型的类
This method uses using pointer equality to compare two references that seem to be of different types. The result of this comparison will always be false at runtime.

  评论这张
 
阅读(374)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018