自定义异常,以assert的形式抛出异常

一、类关联图

二、说明 

1.通过ResponseEnum中定义的异常枚举信息,判断抛出异常;

2.判断方法为公共方法,写在自定义的assert里,代码判断时调用;

3.利用自定义的枚举常量直接可以引用assert里的公共方法,有异常即会抛出自己指定的异常;

 三、源码讲解

 3.1异常类

3.1.1 BaseException类

package com.geo.source.exception.customassert.exception;

import com.geo.source.exception.customassert.IResponseEnum;

/**
 * @author YanZhen
 * @date 2020/05/08 14:25
 **/
public class BaseException extends RuntimeException {
    /**
     * 错误码
     */
    private int errorCode;

    /**
     * 错误信息
     */
    private String errorMessage;

    /**
     * 传参
     */
    private Object errorResult;

    public BaseException(IResponseEnum resp, Object os) {
        super(resp.getMessage());
        this.errorCode = resp.getCode();
        this.errorMessage = resp.getMessage();
        this.errorResult = os;
    }

    public int getErrorCode() {
        return errorCode;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public Object getErrorResult() {
        return errorResult;
    }
}

 3.1.2 BusinessException类

package com.geo.source.exception.customassert.exception;

import com.geo.source.exception.customassert.IResponseEnum;

/**
 * 业务异常
 *
 * @author YanZhen
 * @date 2020/05/08 13:48
 **/
public class BusinessException extends BaseException {
    private static final long serialVersionUID = 1L;

    public BusinessException(IResponseEnum resp) {
        super(resp, null);
    }

    public BusinessException(IResponseEnum resp, Object os) {
        super(resp, os);
    }
}

3.2 assert和响应枚举

3.2.1 CustomAssert 类

package com.geo.source.exception.customassert;

import com.geo.source.exception.customassert.exception.BaseException;

/**
 * 自定义断言
 * @author YanZhen
 * @date 2020/05/08 13:24
 **/
public interface CustomAssert {
    /**
     * 创建异常
     * @return 异常信息
     */
    BaseException newException();

    /**
     * 创建异常
     * @param args 参数
     * @return 异常信息
     */
    BaseException newException(Object args);

    /**
     * 创建异常
     * @param t 异常
     * @param args 参数
     * @return 异常信息
     */
    BaseException newException(Throwable t, Object args);

    /**
     * 是否为空,抛出异常
     * @param o 判断对象
     */
    default void assertNotNull(Object o) {
        if (o == null) {
            throw newException();
        }
    }

    /**
     * 是否为空,抛出异常
     * @param o 判断对象
     * @param args 参数
     */
    default void assertNotNull(Object o, Object args) {
        if (o == null) {
            throw newException(args);
        }
    }
}

3.2.2 IResponseEnum类

package com.geo.source.exception.customassert;

/**
 * 响应信息枚举值模板
 * @author YanZhen
 * @date 2020/05/08 13:44
 **/
public interface IResponseEnum {
    /**
     * 错误编码
     * @return 错误编码
     */
    int getCode();

    /**
     * 错误信息
     * @return 错误信息
     */
    String getMessage();
}

 3.2.3 BusinessAssertException类

package com.geo.source.exception.customassert;

import com.geo.source.exception.customassert.exception.BaseException;
import com.geo.source.exception.customassert.exception.BusinessException;

/**
 * 业务异常断言
 * @author YanZhen
 * @date 2020/05/08 14:07
 **/
public interface BusinessAssertException extends IResponseEnum, CustomAssert {
    @Override
    default BaseException newException() {
        return newException(null, null);
    }

    @Override
    default BaseException newException(Object args) {
        return newException(null, args);
    }

    @Override
    default BaseException newException(Throwable t, Object args) {
        return new BusinessException(this, args);
    }
}

3.3测试

 

package com.geo.source.exception;

import com.geo.source.exception.customassert.exception.BusinessException;
import com.geo.source.exception.exceptinenum.ResponseEnum;
import com.geo.source.exception.simple_example.SimpleBusinessException;
import com.geo.source.exception.simple_example.SimpleResponseEnum;

/**
 * @author YanZhen
 * @date 2020/05/08 14:16
 **/
public class TestMain {
    public static void main(String[] args) {
        v1();
    }

    /**
     * 异常抛出第一版
     */
    private static void v1() {
        try {
            ResponseEnum.LICENCE_NOT_FOUND.assertNotNull(123);
            ResponseEnum.SYSTEM_EXCEPTION.assertNotNull(null, "123");
        } catch (Exception e) {
            BusinessException ex = (BusinessException) e;
            System.out.println(ex.getErrorCode() + " : " + ex.getErrorMessage());
            System.out.println("传入的参数为:" + ex.getErrorResult());
        }
    }
}

 

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值