java异常体系-创建自己项目异常体系[精]

java异常体系-创建自己项目异常体系

1.创建项目异常体系简述

在代码运行中我们会通过异常类来捕获异常信息帮助我们定位问题,查找问题的出现原因。但是java提供的异常类就像是一个模板,不能抛出项目自定义的异常信息出现。因此我们可以根据项目的需求创建一套符合自身项目的异常体系,在运行出错时候抛出我们需要定制化的异常信息。

2. 创建项目异常体系实例

2.1.创建自定义异常类步骤

  1. 创建一个包用于存放我们自定义的异常类。
  2. 创建一个基础的异常类 BaseRuntimeException 继承 RuntimeException 并实现它的构造方法,同时对构造方法进行扩展。
  3. 创建每个业务的异常类,继承基础的异常类 BaseRuntimeException。并实现基础异常类的所有构造方法。
  4. 在业务中使用自定义的异常类。
2.2. 创建异常包和基础异常类
  • 1.异常类代码结构
    在这里插入图片描述
  • 2.创建基础异常类BaseRuntimeException

1.创建基础异常类继承 RuntimeException 异常类。
2.继承 RuntimeException 异常类的所有构造方法。
3. 复制 public BaseRuntimeException(String …msg)
4. 将上面复制的方法进行扩展如下:
public BaseRuntimeException(String …msg) {
//此步骤需要导入下面的guava依赖包
super(Joiner.on("").join(msg));
}

  • 3.导入guava依赖包(gradle.build)
dependencies {
    compile('com.google.guava:guava:28.0-jre')
}
  • 4.基础异常类BaseRuntimeException
package com.test.exection.exception;
import com.google.common.base.Joiner;

public class BaseRuntimeException extends RuntimeException{
    /**
     * 继承RuntimeException 类中的构造方法
     */
    public BaseRuntimeException() {
    }
	
	//1. 复制该方法
    public BaseRuntimeException(String message) {
        super(message);
    }

    // 2. BaseRuntimeException(String message)方法扩展
    public BaseRuntimeException(String ...msg) {
         //此步骤需要导入guava依赖包
        super(Joiner.on("").join(msg));
    }

    public BaseRuntimeException(String message, Throwable cause) {
        super(message, cause);
    }

    public BaseRuntimeException(Throwable cause) {
        super(cause);
    }

    public BaseRuntimeException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }
}
2.3.创建业务异常类

1.创建异常类划分维度:根据项目MVC层结构每层创建一个异常类,异常类的命名规则采用项目每层的名字加异常类型。
例如:项目有server、controller、dao层那么创建三个异常类分别为:ServiceIllegalException、ControllerIllegalException、DaoIllegalException

2.根据项目划分的业务层级关系,每个层级业务都可以创建一个对应的异常类,在出现异常信息时方便定位异常来自哪个业务下的某个类。
3.每个业务异常类都要继承 上面创建的基础异常类

  • 1.创建service层异常类
package com.test.exection.exception;

public class ServiceIllegalException extends BaseRuntimeException {
    public ServiceIllegalException() {
    }

    public ServiceIllegalException(String message) {
        super(message);
    }

    public ServiceIllegalException(String... msg) {
        super(msg);
    }

    public ServiceIllegalException(String message, Throwable cause) {
        super(message, cause);
    }

    public ServiceIllegalException(Throwable cause) {
        super(cause);
    }

    public ServiceIllegalException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }
}
2.4. 业务类中使用自定义的异常类

service层的 ServiceApp 类在抛出异常的时候使用 throw new ServiceIllegalException() 就是创建了ServiceIllegalException() 匿名对象,同时传入多个String类型参数就是调用了我们基础异常类中扩展的BaseRuntimeException(String …msg) 方法。

package com.test.exection.service;

import com.test.exection.exception.ServiceIllegalException;

/**
 * @author bruce
 * @create 2020/4/1 15:25
 */
public class ServiceApp {
    public static void main(String[] args) {
        serviceTest();
    }

    private static void serviceTest() {
        try {
            int i = 1/0;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceIllegalException("{计算结果出现异常:","内部错误码7999}");

        }
    }
}
2.5. 查看自定义异常信息

下面这条异常信息就是我们自定义异常抛出的信息:
Exception in thread "main"com.test.exection.exception.ServiceIllegalException: {计算结果出现异常:内部错误码7999}

java.lang.ArithmeticException: / by zero
	at com.test.exection.ServiceApp.serviceTest(ServiceApp.java:16)
	at com.test.exection.ServiceApp.main(ServiceApp.java:11)
Exception in thread "main" com.test.exection.exception.ServiceIllegalException: {计算结果出现异常:内部错误码7999}
	at com.test.exection.ServiceApp.serviceTest(ServiceApp.java:19)
	at com.test.exection.ServiceApp.main(ServiceApp.java:11)
	```
发布了337 篇原创文章 · 获赞 117 · 访问量 44万+

猜你喜欢

转载自blog.csdn.net/m0_38039437/article/details/105249434
今日推荐