SSM整合以及相关补充( 十 )

  • 项目运行过程中可预计且无法避免的异常
  • 其他异常(Exception)
    • 编程人员未预期到的异常
    对于不同的异常,我们采用不同的应对方法,我们下面做出简单的处理:
    1. Code状态码增加
    package com.itheima.controller;public class Code {    public static final Integer SAVE_OK = 20011;    public static final Integer DELETE_OK = 20021;    public static final Integer UPDATE_OK = 20031;    public static final Integer GET_OK = 20041;    public static final Integer SAVE_ERR = 20010;    public static final Integer DELETE_ERR = 20020;    public static final Integer UPDATE_ERR = 20030;    public static final Integer GET_ERR = 20040;    public static final Integer SYSTEM_ERR = 50001;    public static final Integer SYSTEM_TIMEOUT_ERR = 50002;    public static final Integer SYSTEM_UNKNOW_ERR = 59999;    public static final Integer BUSINESS_ERR = 60002;}
    1. 书写自定义异常处理器
    // SystemExceptionpackage com.itheima.exception;//自定义异常处理器,用于封装异常信息,对异常进行分类,需要继承RuntimeExceptionpublic class SystemException extends RuntimeException{    private Integer code;    public Integer getCode() {        return code;    }    public void setCode(Integer code) {        this.code = code;    }    public SystemException(Integer code, String message) {        super(message);        this.code = code;    }    public SystemException(Integer code, String message, Throwable cause) {        super(message, cause);        this.code = code;    }}// BusinessExceptionpackage com.itheima.exception;//自定义异常处理器,用于封装异常信息,对异常进行分类,需要继承RuntimeExceptionpublic class BusinessException extends RuntimeException{    private Integer code;    public Integer getCode() {        return code;    }    public void setCode(Integer code) {        this.code = code;    }    public BusinessException(Integer code, String message) {        super(message);        this.code = code;    }    public BusinessException(Integer code, String message, Throwable cause) {        super(message, cause);        this.code = code;    }}

    经验总结扩展阅读