项目异常分类:
-
业务异常(BusinessException)
- 规范的用户行为产生的异常
- 不规范的用户行为操作产生的异常
- 方案:发送对应信息传递给用户,提醒规范操作
-
系统异常(SystemException)
- 项目运行过程中可预计且无法避免的异常
- 方案:发送固定消息传递给用户,安抚用户、发送特定消息给运维人员,提醒运维、记录日志
-
其他异常(Exception)
- 编程人员未预期到的异常
- 方案:发送固定消息传递给用户,安抚用户、发送特定消息给编程人员,提醒维护(纳入预期范围内)、记录日志
- 首先定义一个Code类,用来定义错误代码
public class Code {
public static final Integer SYSTEM_ERR=50001;
public static final Integer SYSTEM_TIMEOUT_ERR=50002;
public static final Integer BUSINESS_ERR=60001;
public static final Integer SYSTEM_UNKNOW_ERR=5999;
}
2.定义一个json的返回结果集类
public class Result {
private Object data;
private Integer code;
private String msg;
public Result(Integer code,Object data, String msg) {
this.data = data;
this.code = code;
this.msg = msg;
}
public Result(Integer code,Object data) {
this.data = data;
this.code = code;
}
public Result() {
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
3.定义业务异常处理类BussinessException,此处模拟的是超时异常,故继承了RuntimeException
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, String message) {
super(message);
this.code = code;
}
public BussinessException(Integer code,String message, Throwable cause) {
super(message, cause);
this.code = code;
}
}
- 定义系统异常处理类SystemException,此处模拟的是超时异常,故继承了RuntimeException
public 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;
}
}
- 在getById(Integer id)方法中模拟异常
@Override
public Book getById(Integer id) {
//模拟异常
if (id==1){
throw new BussinessException(Code.BUSINESS_ERR,"请不要使用你的技术挑战我的耐心!");
}
//将可能出现的异常进行包装,转换成自定义异常
try {
int i=1/0;
}catch (Exception e){
throw new SystemException(Code.SYSTEM_UNKNOW_ERR,"服务器访问超时,请重试!",e);
}
return bookDao.getById(id);
}
- 编写异常处理类ProjectExeptionAdvice,注意在SpringMvcConfig类中定义的扫描范围要包含ProjectExeptionAdvice类
@RestControllerAdvice//声明这是处理Rest风格开发的controller
public class ProjectExeptionAdvice {
//系统异常
@ExceptionHandler(SystemException.class)
public Result doSystemException(SystemException se){
//记录日志
//发送消息给运维
//发送邮件给开发人员
return new Result(se.getCode(),null,se.getMessage());
}
//业务异常
@ExceptionHandler(BussinessException.class)
public Result doBussinessException(BussinessException be){
return new Result(be.getCode(),null,be.getMessage());
}
//其他异常
@ExceptionHandler(Exception.class)
public Result doException(Exception e){
//记录日志
//发送消息给运维
//发送邮件给开发人员,e对象发给开发人员
return new Result(Code.SYSTEM_UNKNOW_ERR,null,"系统异常,请稍后再试!");
}
}
- 测试模拟的异常处理