自定义业务异常


在开发项目的工程中,经常需要定义属于自己系统的业务异常,所以这里做一个记录分享。

一、继承RuntimeException

maven依赖:

   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
    </parent>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.6.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.6.0</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

/**
 * 业务异常
 * @author fck
 */
public class BusinessException extends RuntimeException{

    private Integer code;
    private String message;
    public BusinessException() {
    }

    public BusinessException(Integer code, String message) {
        super(message);
        this.code = code;
    }

    public Integer getCode() {
        return code;
    }

    public static BusinessException build(Integer code, String message){
        return new BusinessException(code,message);
    }

    @Override
    public synchronized Throwable fillInStackTrace() {
        return this;
    }
}

二、自定义异常常量

/**
 * 异常常量
 * @author fck
 */
public interface ExceptionConst {

    /**
     * 当前系统的异常码10001 - 100000
     */
    interface CurrentSystem{
        BusinessException SYSTEM_ERROR = BusinessException.build(10001,"系统异常,请联系管理员");
        BusinessException VALIDATE_ERROR = BusinessException.build(10002,"入参校验异常");
    }
    /**
     * 客户信息异常码 100001 - 100090
     * @author wangxw
     */
    interface EnpterPriseException{
        BusinessException ENTP_EMPTY_EXCEPTION = BusinessException.build(100001,"未获取到用户信息");
        BusinessException ENTP_ALLOW_FILE_MAX_EXCEPTION = BusinessException.build(100002,"用户信息最大不得超过20M");
        BusinessException ENTP_INIT_USER_INFO_EMPTY_EXCEPTION = BusinessException.build(100003,"初始化用户信息失败");
        BusinessException ENTP_REG_USER_EXIST_EXCEPTION = BusinessException.build(100004,"注册用户已存在用户表中");

    }

}

测试

这里就简单做个测试示例:

/**
 * @author fck
 * @version 1.0
 * @date 2021/11/2 15:03
 */
@Api("自定义业务异常测试")
@RestController
public class BusinessExceptionController {

    @GetMapping("/test")
    public String getName(String name){
        if (StringUtils.isEmpty(name)){
            throw ExceptionConst.CurrentSystem.SYSTEM_ERROR;
        }
        return name;
    }
}

在这里插入图片描述

在这里插入图片描述

总结

根据不同的业务系统,可以定义不同的系统常量,来满足需求所需,各位小伙伴可各取所需。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值