`
scholers
  • 浏览: 613831 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

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

 
阅读更多
REC_CATCH_EXCEPTION




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.比较推荐的写法一般如下:
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





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.
  • 大小: 153.5 KB
  • 大小: 146 KB
分享到:
评论

相关推荐

    fingbugs Java Bug检查工具

    Findbugs工具使用方便,检查java代码bug能力极强,是Java程序员必备的工具之一。

    fingbugs使用

    fingbugsfingbugsfingbugs

    fingbugs.zip

    FindBugs 是一个静态分析工具,它检查类或者 JAR 文件,将字节码与一组缺陷模式进行对比以发现可能...不是通过分析类文件的形式或结构来确定程序的意图,而是通常使用 Visitor 模式来鉴别代码是否符合一些固定的规范。

    findbugs应用eclipse的插件

    findbugs 代码检查 eclipse插件 3.0.0版本

    findbugs eclipse插件

    最新版的fingbugs eclipse plugin,08年5月份更新的,超好用的静态分析工具。

    findbugs的架包

    findbugs的架包。直接放入eclipse中的plugin包中,然后重启eclipse就可以看到。

Global site tag (gtag.js) - Google Analytics