异常捕捉之自定义异常类

自定义异常类是为了在ajax增、删、改的时候返回异常参数给客户端,以便程序按照程序员的预期正常执行!

比如ajax请求修改在Controller中如此写:

 /**
     * 修改政策法规数据记录
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public AjaxCommonObject update(@RequestBody CarPolicies dto) {

        AjaxCommonObject ajaxCommonObject = new AjaxCommonObject();
        try {
           bizService.update(dto);
        } catch (BizCommonException e) {
            return new AjaxCommonObject(e);
        }
        return ajaxCommonObject;
    }

其中AjaxCommonObject就是我们自定义的异常类:

public class AjaxCommonObject {
    private int code;
    private String message;
    private Object data;

    public AjaxCommonObject() {
        this.code = 0;
        this.message = "ajax业务请求正确响应";
    }

    public AjaxCommonObject(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public AjaxCommonObject(BizCommonException bizCommonException) {
        this.code = bizCommonException.getCode();
        this.message = bizCommonException.getMessage();
    }

    public AjaxCommonObject(BizCommonExceptionEnum bizCommonExceptionEnum) {
        this.code = bizCommonExceptionEnum.getCode();
        this.message = bizCommonExceptionEnum.getMessage();
    }

    public int getCode() {
        return this.code;
    }

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

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return this.data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}

其中在此类中有两个构造:

    public AjaxCommonObject(BizCommonException bizCommonException) {
        this.code = bizCommonException.getCode();
        this.message = bizCommonException.getMessage();
    }

    public AjaxCommonObject(BizCommonExceptionEnum bizCommonExceptionEnum) {
        this.code = bizCommonExceptionEnum.getCode();
        this.message = bizCommonExceptionEnum.getMessage();
    }

一、在BizCommonException类中要继承RuntimeException:

public class BizCommonException extends RuntimeException {
    private int code;
    private String message;

    public BizCommonException(int code, String message) {
        super(message);
        this.code = code;
        this.message = message;
    }

    public BizCommonException(BizCommonExceptionEnum bizCommonExceptionEnum) {
        this.code = bizCommonExceptionEnum.getCode();
        this.message = bizCommonExceptionEnum.getMessage();
    }

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getCode() {
        return this.code;
    }

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

 二、BizCommonExceptionEnum 确定一些枚举类型,以便使用:

public enum BizCommonExceptionEnum {
    DICT_EXISTED(400, "字典已经存在"),
    DICT_NOT_EXISTED(400, "字典不存在"),
    UPLOAD_ERROR(500, "上传图片出错"),
    DB_RESOURCE_NULL(400, "数据库中没有该资源"),
    NO_PERMITION(405, "权限异常"),
    REQUEST_INVALIDATE(400, "请求数据格式不正确"),
    INVALID_KAPTCHA(400, "验证码不正确"),
    USER_ALREADY_REG(401, "该用户已经注册"),
    NO_THIS_USER(400, "没有此用户"),
    USER_NOT_EXISTED(400, "没有此用户"),
    ROLE_NOT_EXISTED(400, "没有此角色"),
    ACCOUNT_FREEZED(401, "账号被冻结"),
    OLD_PWD_NOT_RIGHT(402, "原密码不正确"),
    TWO_PWD_NOT_MATCH(405, "两次输入密码不一致"),
    EXISTED_THE_MENU(400, "菜单编号重复,不能添加"),
    EXISTED_THE_MENUNAME(400, "菜单名称重复"),
    EXISTED_THE_ORGNAME(400, "组织名称重复"),
    REQUEST_NULL(400, "请求数据格式错误,并且必传参数不能为空"),
    SESSION_TIMEOUT(400, "会话超时"),
    SERVER_ERROR(500, "服务器异常,请联系系统运维人员"),
    EXISTED_THE_RECORD(406, "数据已经存在,不能重复操作"),
    SAVE_DATA_ERROR(407, "数据保存失败,请联系运维人员或检查网络稍后再试!");

    private int code;
    private String message;

    private BizCommonExceptionEnum(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public int getCode() {
        return this.code;
    }

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

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值