文章目录
异常处理器
异常处理器快速入门
程序开发过程中不可避免的会遇到异常现象
出现异常现象的常见位置与常见原因如下:
各个层级均出现异常,异常处理代码书写在哪一层都是不合适的
表现层处理异常,如果每个方法中都单独书写try…catch…,代码书写量巨大且意义不强
SpringMVC提供了异常处理器:
@RestControllerAdvice类注解
@ExceptionHandler方法注解:
@RestControllerAdvice // 声明该类是用来做异常处理的
public class ProjectExceptionAdvice {
@ExceptionHandler(Exception.class) // 声明该方法是拦截所有异常
public Result doException(Exception ex) {
// 返回异常提示
return new Result(666, null, "出现异常");
}
}
项目异常处理
项目中的异常不能和上面的处理方式一样
项目异常分类:
项目异常处理方案:
使用步骤:
- 创建一个exception包, 在该包下分别创建业务异常类和系统异常类:
public class BusinessException extends RuntimeException {
// 添加一个编号用于识别异常
private Integer 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;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
}
public class SystemException extends RuntimeException{
// 添加一个编号用于识别异常
private Integer 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;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
}
- 自定义异常编码(持续补充)
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 SELECT_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 SELECT_ERR = 20040;
public static final Integer SYSTEM_ERROR = 50001; // 系统异常
public static final Integer SYSTEM_TIMEOUT_ERROR = 50002; // 请求超时
public static final Integer SYSTEM_UNKNOW_ERROR = 59999; // 系统未知异常
public static final Integer BUSINESS_ERROR = 60001; // 业务异常
}
- 触发自定义异常: 将可能出现的转换为自定义异常
@GetMapping("/{id}")
public Result selectById(@PathVariable Integer id) {
try {
int i = 1/0;
} catch (Exception e) {
throw new SystemException(Code.SYSTEM_TIMEOUT_ERROR, "服务器访问超时, 请稍后重试", e);
}
Book book = bookService.selectById(id);
boolean flag = book != null;
Integer code = flag ? Code.SELECT_OK: Code.SELECT_ERR;
String msg = flag ? "": "数据查询失败, 请重试!";
return new Result(code, book, msg);
}
@GetMapping("/{id}")
public Result selectById(@PathVariable Integer id) {
if (id < 0) {
throw new BusinessException(Code.BUSINESS_ERROR, "请进行合法的操作");
}
Book book = bookService.selectById(id);
boolean flag = book != null;
Integer code = flag ? Code.SELECT_OK: Code.SELECT_ERR;
String msg = flag ? "": "数据查询失败, 请重试!";
return new Result(code, book, msg);
}
- 处理器拦截系统异常和业务异常并处理
@RestControllerAdvice // 声明该类是用来做异常处理的
public class ProjectExceptionAdvice {
@ExceptionHandler(SystemException.class) // 拦截系统异常
public Result doSystemException(SystemException e) {
// 记录日志...
// 发送消息给运维...
// 发送邮件给开发人员..
// 返回异常提示
return new Result(e.getCode(), null, e.getMessage());
}
@ExceptionHandler(BusinessException.class) // 拦截业务异常
public Result doBusinessException(BusinessException e) {
// 返回异常提示
return new Result(e.getCode(), null, e.getMessage());
}
@ExceptionHandler(Exception.class) // 其他异常: 拦截所有的异常
public Result doException(Exception ex) {
// 记录日志...
// 发送消息给运维...
// 发送邮件给开发人员..
// 返回异常提示
return new Result(Code.SYSTEM_UNKNOW_ERROR, null, "系统繁忙请稍后再试");
}
}