关于java中自定义异常怎么定义

版权声明:转载请注明出处 https://blog.csdn.net/chenmingxu438521/article/details/90029444

一、背景

1.项目中的异常肯定是必须的,定义好结构良好的异常类,可以快速的定位生产bug的位置。

二、异常码枚举

1.先定义一个异常码枚举处理

public enum ErrorCodeEnum {

    SYS_ERROR("SYS_ERROR","系统错误,请重试"),
    UNKNOWN_ERROR("UNKNOWN_ERROR","未知的系统异常"),
    SERVICE_INVOKE_FAIL("SERVICE_INVOKE_FAIL","服务器调用失败"),
    ILLEGAL_ARGS("ILLEGAL_ARGS","参数校验错误");

    //结果码值
    private String code;
    //描述
    private String desc;

    ErrorCodeEnum(String code, String desc) {
        this.code=code;
        this.desc=desc;
    }
    //还有一个根据枚举CODE去找相应枚举值,这个在实际开发中非常常用
    public static ErrorCodeEnum getByValue(String code) {
        for (ErrorCodeEnum result : ErrorCodeEnum.values()) {
            if (StringUtils.equals(result.code,code)) {
                return result;
            }
        }
        return null;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
}

三、自定义异常

1.注意有个异常枚举。

2.继承了RuntimeException。

3.个构造方法,还可以增加其他默认构造方法。一个完全根据异常码枚举的、一个是异常码枚举和异常描述。

public class CustomException extends RuntimeException {

    private static final long serialVersionUID=1L;
    //错误码
    private ErrorCodeEnum errorCode;
    //带参构造器,模版异常的构造方法
    public CustomException(ErrorCodeEnum errorCode){
        super(errorCode.getDesc());
        this.setErrorCode(errorCode);
    }
    //带自定义异常信息的构造方法
    public CustomException(ErrorCodeEnum errorCode,String message){
        super(StringUtils.isNotBlank(message)?message:errorCode.getDesc());
        this.setErrorCode(errorCode);
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public ErrorCodeEnum getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(ErrorCodeEnum errorCode) {
        this.errorCode = errorCode;
    }
}

四、测试类

1.这里的catch可以封装一个result对象,把异常码、异常描述、成功与否封装进去

public class ExcepyionTest {
    public static void main(String[] args) {
        //String name=null;
        int i =0;
        try {
            /*if (name == null){
                throw new CustomException(ErrorCodeEnum.ILLEGAL_ARGS);
            }*/
            if (i==0){
                throw new CustomException(ErrorCodeEnum.ILLEGAL_ARGS,"参数不能为0");
            }

        }catch (CustomException e){
            e.printStackTrace();
            System.out.println("异常码"+e.getErrorCode().getCode());
            System.out.println("异常描述"+e.getMessage());
        }
    }
}

五、结果

1.第一个异常

异常码ILLEGAL_ARGS
com.example.demo.CustomException: 参数校验错误
异常描述参数校验错误
	at com.example.demo.ExcepyionTest.main(ExcepyionTest.java:9)

2.把测试类中的注释

异常码ILLEGAL_ARGS
com.example.demo.CustomException: 参数不能为0
异常描述参数不能为0
	at com.example.demo.ExcepyionTest.main(ExcepyionTest.java:10)

六、总结

1.定义异常类就可以,如果业务非常复杂可以把异常码类多搞几个出来。

2.项目中一个异常类够用了,通过不同异常码定义去区分业务异常。

3.强烈建议开发时定义异常码,越细越好,上了生产方便快速定位bug。

七、结束

Always keep the faith!!!

猜你喜欢

转载自blog.csdn.net/chenmingxu438521/article/details/90029444