从 FingBugs的错误来看JAVA代码质量(五)
来源:互联网 发布:ipad淘宝旧版本下载 编辑:程序博客网 时间:2024/06/05 16:55
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.比较推荐的写法一般如下:
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.
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.
阅读全文
0 0
- 从 FingBugs的错误来看JAVA代码质量(五)
- 从 FingBugs的错误来看JAVA代码质量(五)
- 从 FingBugs的错误来看JAVA代码质量(一)
- 从 FingBugs的错误来看JAVA代码质量(二)
- 从 FingBugs的错误来看JAVA代码质量(三)
- 从 FingBugs的错误来看JAVA代码质量(四)
- 从 FingBugs的错误来看JAVA代码质量(一)
- 从 FingBugs的错误来看JAVA代码质量(二)
- 从 FingBugs的错误来看JAVA代码质量(三)
- 从 FingBugs的错误来看JAVA代码质量(四)
- 从 FingBugs的错误来看JAVA代码质量
- 从 FingBugs的错误来看JAVA代码质量
- 从Java类初始化,来看代码优化
- 从代码来看block
- 从ssh来看面向对象的五大原则
- 从五个维度考察java代码质量
- 编写高质量iOS代码(五)
- 代码分析你的网站从SEO的角度来看
- linux下CS并发服务器开发方案
- Java+Selenium3方法篇47-字符串切片操作
- 微信小程序三级联动地址选择器
- 哈喽摩托
- Spark Streaming编程指南
- 从 FingBugs的错误来看JAVA代码质量(五)
- 读书笔记《你不知道的JavaScript上卷》1.4提升
- SPOJ-1029-二维树状数组
- 对ScrollView 的getScrollY 的体会
- 欧拉回路+无极卡常——51nod1967 路径定向
- 《android按钮点击事件的频繁点击问题处理》
- 关于C#中{get;set;}与{get:return;set:value}的区别
- 数据库查询
- 实现table中tr、td的可移动效果