异常处理
项目异常处理方案
异常分类
业务异常
规范的用户行为产生的异常
不规范的用户行为操作产生的异常
系统异常
项目运行过程中可预计且无法避免的异常
其他异常
编程人员未预期到的异常
异常处理方案
业务异常
发送对应的消息传递给用户,提醒规范操作
系统异常
发送固定消息传递给用户,安抚用户
发送特定的消息给运维人员,提醒维护
记录日志
其他异常
发送固定消息传递给用户,安抚用户
发送特定消息给编程人员,提醒维护
纳入预期范围内
记录日志
自定义异常
业务异常
package com.test.springmvc.exception;
/**
* @author zhangzengxiu
* @date 2021/11/29
*/
public class BussinessException extends RuntimeException {
private Integer code;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public BussinessException(Integer code) {
this.code = code;
}
public BussinessException(String message,Integer code) {
super(message);
this.code = code;
}
public BussinessException(String message, Throwable cause,Integer code) {
super(message, cause);
this.code = code;
}
public BussinessException(Throwable cause,Integer code) {
super(cause);
this.code = code;
}
public BussinessException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace,Integer code) {
super(message, cause, enableSuppression, writableStackTrace);
this.code = code;
}
}
系统异常
package com.test.springmvc.exception;
/**
* @author zhangzengxiu
* @date 2021/11/29
*/
public class SystemException extends RuntimeException {
public SystemException() {
}
public SystemException(String message) {
super(message);
}
public SystemException(String message, Throwable cause) {
super(message, cause);
}
public SystemException(Throwable cause) {
super(cause);
}
public SystemException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
异常处理器:
package com.test.springmvc.exception;
import com.test.springmvc.result.Result;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* 异常处理器
*
* @author zhangzengxiu
* @date 2021/11/29
*/
@ControllerAdvice
@Component
public class ExceptionAdvice {
@ResponseBody
@ExceptionHandler(BussinessException.class)
public Result doBussinessException(BussinessException e) {
return new Result(e.getCode(), e.getMessage());
}
}
Controller:
@GetMapping("/findById/{id}")
public Result<User> findById(@PathVariable("id") int id) {
User user = userService.findById(id);
if (user == null) {
throw new BussinessException(Status.GET_ERR.getCode());
}
return new Result(Status.GET_OK.getCode(), Status.GET_OK.getMessage(), user);
}
实际企业开发过程中,Controller不会再抛异常出去,会在这层做try{}catch(){},抓住然后进行统一处理
参考博客
如下:
@GetMapping("/findById/{id}")
public Result<User> findById(@PathVariable("id") int id) {
try {
User user = userService.findById(id);
if (user == null) {
return new Result(Status.GET_ERR.getCode(), Status.GET_ERR.getMessage());
}
return new Result(Status.GET_OK.getCode(), Status.GET_OK.getMessage(), user);
} catch (Exception e) {
return new Result(Status.GET_ERR.getCode(), Status.GET_ERR.getMessage());
}
}