异常处理流程

@ControllerAdvice
public class BaseExceptionHandler {
@ExceptionHandler(value = Exception.class)
@ResponseBody
public Result error(Exception e){
e.printStackTrace();
return new Result(false, StatusCode.ERROR, "系统正忙!程序员正在骑马赶来的路上!");
}
@ExceptionHandler(value = CustomException.class)
@ResponseBody
public Result custom(CustomException e){
e.printStackTrace();
return new Result(false, StatusCode.ERROR, e.getMessage());
}
}
public class CustomException extends RuntimeException {
private String message;
@Override
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public CustomException(String message) {
this.message = message;
}
public CustomException() {
}
}
本文介绍了一种基于Spring框架的异常处理机制,通过自定义异常类和控制器建议类实现统一的错误响应格式,增强了系统的稳定性和用户体验。
1525

被折叠的 条评论
为什么被折叠?



