• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >Java > Java代码中常见技术债务处理之Exception

Java代码中常见技术债务处理之Exception

作者:高效软件开发 字体:[增加 减小] 来源:互联网 时间:2017-07-23

高效软件开发通过本文主要向大家介绍了异常,java等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

写在前面

异常处理是代码中常见的处理,本文根据SonarQube在异常方面的规则和常见检查结果,选取说明了常见异常处理中的技术债务,提倡技术债务最少的编码方式。

Exception handlers should preserve the original exceptions

Either log or rethrow this exception.

When handling a caught exception, the original exception’s message and stack trace should be logged or passed forward.
NONCOMPLIANT CODE EXAMPLE

 // Noncompliant - exception is lost
try { /* ... */ } catch (Exception e) { LOGGER.info("context"); }   

// Noncompliant - exception is lost (only message is preserved)       
try { /* ... */ } catch (Exception e) { LOGGER.info(e.getMessage()); }   

// Noncompliant - exception is lost 
try { /* ... */ } catch (Exception e) { throw new RuntimeException("context"); }

COMPLIANT SOLUTION

try { /* ... */ } catch (Exception e) { LOGGER.info(e); }   

try { /* ... */ } catch (Exception e) { throw new RuntimeException(e); }

try {
  /* ... */
} catch (RuntimeException e) {
  doSomething();
  throw e;
} catch (Exception e) {
  // Conversion into unchecked exception is also allowed
  throw new RuntimeException(e);
}

错误实例:

 protected int analyzeJobStep1(String jobName) {
        try {
            notifyBHandler();
            return analyzeJobStep2(jobName)
        } catch (Exception e) {
            logger.error(e.getMessage();
        }
         return 0;
    }

解决实例:
    protected int analyzeJobStep1(String jobName) {
        int nRet=0;
        try {
            notifyBHandler();
            nRet = analyzeJobStep2(jobName);
        } catch (Exception e) {
            logger.error("notifyBHandler trigger exception", e);
        }
        return nRet;
    }

Don’t directly use Exception and RuntimeException

Sonarrule:Generic exceptions should never be thrown (squid:S00112)
Using such generic exceptions as Error, RuntimeException, Throwable, and Exception prevents calling methods from handling true, system-generated exceptions differently than application-generated errors.
Noncompliant Code Example

public void foo(String bar) throws Throwable {  // Noncompliant
  throw new RuntimeException("My Message");     // Noncompliant
}

Compliant Solution

public void foo(String bar) {
  throw new MyOwnRuntimeException("My Message"); 
}

Another related rule: Generic exceptions should never be thrown
the below should be avoided.

@Override
public void myMethod() throws Exception {...}

Define and throw a dedicated exception instead of using a generic one.

Exceptions should not be thrown in finally blocks

try {
  /* some work which end up throwing an exception */
  throw new IllegalArgumentException();
} finally {
  /* clean up */
  throw new RuntimeException();       // Noncompliant; will mask the IllegalArgumentException
}

Compliant Solution

try {
  /* some work which end up throwing an exception */
  throw new IllegalArgumentException();
} finally {
  /* clean up */                                         // Compliant
}

Checked exceptions should not be thrown

The purpose of checked exceptions is to ensure that errors will be dealt with, either by propagating them or by handling them, but some believe that checked exceptions negatively impact the readability of source code, by spreading this error handling/propagation logic everywhere.
This rule verifies that no method throws a new checked exception.

CODE EXAMPLE

public void myMethod1() throws CheckedException {
  ...
  throw new CheckedException(message);   // Noncompliant
  ...
  throw new IllegalArgumentException(message); // Compliant; IllegalArgumentException is unchecked
}   

Solution Example

public void myMethod2() throws CheckedException {  // Compliant; propagation allowed
  myMethod1();
}

Public methods should throw at most one checked exception

Using checked exceptions forces method callers to deal with errors, either by propagating them or by handling them. Throwing exceptions makes them fully part of the API of the method.
But to keep the complexity for callers reasonable, methods should not throw more than one kind of checked exception.

NONCOMPLIANT CODE EXAMPLE

public void delete() throws IOException, SQLException {      // Noncompliant
  /* ... */
}

COMPLIANT SOLUTION


 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • Java代码中常见技术债务处理之Exception
  • springboot全局异常处理详解
  • SpringMVC实现自定义类型转换器
  • java异常处理详细介绍及实例
  • JAVA用户自定义事件监听实例代码
  • 深入理解java异常处理机制的原理和开发应用
  • SpringBoot初始教程之统一异常处理详解
  • 详解Springboot自定义异常处理
  • Java 常用类解析:java异常机制,异常栈,异常处理方式,异常链,异常丢失详解
  • springboot全局异常处理详解

相关文章

  • 2017-05-28SWT(JFace)Group(分组显示)
  • 2017-05-28RandomAccessFile简介_动力节点Java学院整理
  • 2018-11-21JBoss中事务超时的解决方案
  • 2017-05-28Java中的InputStreamReader和OutputStreamWriter源码分析_动力节点Java学院整理
  • 2017-05-28Java中SpringSecurity密码错误5次锁定用户的实现方法
  • 2017-05-28java this 用法详解及简单实例
  • 2017-05-28Spring Boot定时任务的使用实例代码
  • 2017-05-28Spring 数据库连接池(JDBC)详解
  • 2017-05-28Java 爬虫工具Jsoup详解
  • 2017-05-28Ubuntu快速安装jdk的教程

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • SpringMVC中解决@ResponseBody注解返回中文乱码问题
    • Java集合之HashMap用法详解
    • Java 通过位运算求一个集合的所有子集方法
    • 推荐几个学习java的网站
    • Java编写中容易搞错的一些东西
    • java实现将汉语转换为拼音功能
    • Java Calendar类常用示例_动力节点Java学院整理
    • JVM(Java虚拟机)简介(动力节点Java学院整理)
    • 老生常谈JVM的内存溢出说明及参数调整
    • Java 生产者/消费者问题实例详解

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有