| 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; |
| |
| } |
| public 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; |
| } |
| |
| } |
| 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; |
| } |
| |
| } |
| public Book getById(Integer id) { |
| |
| if(id == 1){ |
| throw new BusinessException(Code.BUSINESS_ERR,"请不要使用你的技术挑战我的耐性!"); |
| } |
| |
| try{ |
| int i = 1/0; |
| }catch (Exception e){ |
| throw new SystemException(Code.SYSTEM_TIMEOUT_ERR,"服务器访问超时,请重试!",e); |
| } |
| return bookDao.getById(id); |
| } |
| |
| @RestControllerAdvice |
| public class ProjectExceptionAdvice { |
| |
| |
| |
| @ExceptionHandler(SystemException.class) |
| public Result doSystemException(SystemException ex){ |
| |
| |
| |
| return new Result(ex.getCode(),null,ex.getMessage()); |
| } |
| |
| |
| @ExceptionHandler(BusinessException.class) |
| public Result doBusinessException(BusinessException ex){ |
| return new Result(ex.getCode(),null,ex.getMessage()); |
| } |
| |
| |
| @ExceptionHandler(Exception.class) |
| public Result doOtherException(Exception ex){ |
| |
| |
| |
| return new Result(Code.SYSTEM_UNKNOW_ERR,null,"系统繁忙,请稍后再试!"); |
| } |
| |
| } |
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术