定义系统未知异常,运行时异常,客户使用时异常
自定义异常编码
- 提供控制或业务层调用,返回给前端或异常处理
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_UNKNWO_ERR=5900;
public static final Integer SYSTEM_TIMEOUT_ERR=5910;
public static final Integer BUSINESS_ERR=5920;
}
自定义定义code码方便与前端交互
1. 系统运行异常
- 成员变量code,方便后面再最终的全局异常中使用
public class SystemException extends RuntimeException{
private Integer code;
public SystemException(Integer code,String message) {
super(message);
this.code = code;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public SystemException(Integer code, String message, Throwable cause) {
super(message, cause);
this.code = code;
}
}
定义了一个名为
SystemException
的类,它继承自RuntimeException
。这个类用于表示系统中的一种异常,其中包含了两个主要特性:一个错误码(code
)和一个描述性消息(通过继承自RuntimeException
的message
)。此外,提供了两个构造函数来创建SystemException
实例,一个接受错误码和消息,另一个还额外接受一个Throwable
类型的cause
参数来表示异常的原因。
2. 用户异常
public class BusinessException extends RuntimeException{
private Integer code;
public BusinessException(Integer code, String message) {
super(message);
this.code = code;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public BusinessException(Integer code, String message, Throwable cause) {
super(message, cause);
this.code = code;
}
}
同上
3. 全局异常处理
- 集中处理系统异常,运行异常,用户异常向上抛出
@RestControllerAdvice
public class ProjectExceptionAdvice {
@ExceptionHandler(Exception.class)
public Result doException(Exception exception){
//记录日志
//发送消息给运维人员
//发送邮件给开发人员 发送ex对象
return new Result(Code.SYSTEM_UNKNWO_ERR,null,"系统繁忙");
}
@ExceptionHandler(SystemException.class)
public Result doSystemException(SystemException exception){
//记录日志
//发送消息给运维人员
//发送邮件给开发人员 发送ex对象
return new Result(exception.getCode(),null, exception.getMessage());
}
@ExceptionHandler({BusinessException.class})
public Result doBusinessException(BusinessException exception){
return new Result(exception.getCode(),null,exception.getMessage());
}
定义了一个名为
ProjectExceptionAdvice
的类,它使用了@RestControllerAdvice
注解来全局处理控制器中抛出的异常。这个类通过定义多个@ExceptionHandler
方法来捕获并处理不同类型的异常,包括Exception
(所有异常的基类)、SystemException
(自定义的系统异常)和BusinessException
(自定义的业务异常)。
模拟异常:
controller层模拟异常
- 此时抛出的是运行异常
@GetMapping("/select")
public Result selectAll(){
List<Book> books = bookService.selectAll();
Integer code = books != null ? Code.SELECT_OK : Code.SELECT_ERR;
String msg = books != null ? "查询成功" :" 查询失败";
if (true) {
throw new SystemException(Code.SYSTEM_TIMEOUT_ERR,"模拟异常");
}
return new Result(code,books,msg);
}
执行过程:
1. 此时程序执行到异常这里,抛出SystemException这个异常。
2. 向SystemException类传入code异常码,和msg异常信息。给code变量赋值
3. 再被全局异常处理器ProjectExceptionAdvice接收调用此类中,对应的自定义异常处理方法4. doSystemException。得到最开始controller层传入的code异常码和msg异常信息返回出去
- 前端显示