当前位置:网站首页>3-全局异常处理
3-全局异常处理
2022-07-29 06:52:00 【张 邵】
异常处理(SpringMVC知识)
了解异常
SpringMVC 注解方式处理
该处理方式是在 核心控制器加载的时候就加载了,所以可以拦截到参数中的异常
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;
@Component
//使用注解开发异常处理器
//声明该类是一个Controller的通知类,声明后该类就会被加载成异常处理器
//可以在controller的前后做功能增强
@ControllerAdvice
public class ExceptionAdvice {
//类中定义的方法携带@ExceptionHandler注解的会被作为异常处理器,后面添加实际处理的异常类型
@ExceptionHandler(NullPointerException.class)
@ResponseBody
public String doNullException(Exception ex){
return "空指针异常";
}
@ExceptionHandler(ArithmeticException.class)
@ResponseBody
public String doArithmeticException(Exception ex){
return "ArithmeticException";
}
@ExceptionHandler(Exception.class)
@ResponseBody
public String doException(Exception ex){
return "all";
}
}
项目异常处理方案【理解】


自定义异常
业务异常(用户) BusinessException
//自定义异常继承RuntimeException,覆盖父类所有的构造方法
public class BusinessException extends RuntimeException {
public BusinessException() {
}
public BusinessException(String message) {
super(message);
}
public BusinessException(String message, Throwable cause) {
super(message, cause);
}
public BusinessException(Throwable cause) {
super(cause);
}
public BusinessException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
系统异常: SystemException
//自定义异常继承RuntimeException,覆盖父类所有的构造方法
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);
}
}
其他异常: DevelopException
//自定义异常继承RuntimeException,覆盖父类所有的构造方法
public class DevelopException extends RuntimeException {
public DevelopException() {
}
public DevelopException(String message) {
super(message);
}
public DevelopException(String message, Throwable cause) {
super(message, cause);
}
public DevelopException(Throwable cause) {
super(cause);
}
public DevelopException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
将异常包装为自定义异常
public void save(){
//业务层中如果出现了异常,就对出现异常的代码进行try...catch...处理
//在catch中将出现的异常包装成自定义异常,同时务必将当前异常对象传入自定义异常,避免真正的异常信息消失
try {
throw new SQLException();
} catch (SQLException e) {
throw new SystemException("数据库连接超时!",e);
}
}
统一处理异常
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@Component
@ControllerAdvice
public class ProjectExceptionAdvice {
@ExceptionHandler(BusinessException.class)
@ResponseBody()
public String doBusinessException(Exception ex, Model m){
//使用参数Model将要保存的数据传递到页面上,功能等同于ModelAndView
//业务异常出现的消息要发送给用户查看
// m.addAttribute("msg",ex.getMessage());
return ex.getMessage();
}
@ExceptionHandler(value = {
SystemException.class})
@ResponseBody
public String doSystemException(Exception ex, Model m){
//系统异常出现的消息不要发送给用户查看,发送统一的信息给用户看
// m.addAttribute("msg","服务器出现问题,请联系管理员!");
//实际的问题现象应该传递给redis服务器,运维人员通过后台系统查看
//实际的问题显现更应该传递给redis服务器,运维人员通过后台系统查看
return "服务器出现问题,请联系管理员!";
}
@ExceptionHandler(Exception.class)
@ResponseBody
public String doException(Exception ex, Model m){
// m.addAttribute("msg",ex.getMessage());
//将ex对象保存起来
return ex.getMessage();
}
}
统一异常处理
@Component
@ControllerAdvice
@Log4j2
public class ProjectExceptionAdvice {
@ExceptionHandler(BusinessException.class)
@ResponseBody()
public ResponseResult doBusinessException(Exception exception, Model m){
//使用参数Model将要保存的数据传递到页面上,功能等同于ModelAndView
//业务异常出现的消息要发送给用户查看
// m.addAttribute("msg",ex.getMessage());
// return ex.getMessage();
log.error("catch exception:{}", exception.getMessage());
return ResponseResult.errorResult(500,exception.getMessage());
}
@ExceptionHandler(value = {
SystemException.class})
@ResponseBody
public ResponseResult doSystemException(Exception exception, Model m){
//系统异常出现的消息不要发送给用户查看,发送统一的信息给用户看
// m.addAttribute("msg","服务器出现问题,请联系管理员!");
//实际的问题现象应该传递给redis服务器,运维人员通过后台系统查看
//实际的问题显现更应该传递给redis服务器,运维人员通过后台系统查看
// return "服务器出现问题,请联系管理员!";
log.error("catch exception:{}", exception.getMessage());
return ResponseResult.errorResult(503,"服务器出现问题,请联系管理员!");
}
@ExceptionHandler(Exception.class)
@ResponseBody
public ResponseResult doException(Exception exception, Model m){
// m.addAttribute("msg",ex.getMessage());
//将ex对象保存起来
//记录日志
exception.printStackTrace();
log.error("catch exception:{}", exception.getMessage());
return ResponseResult.errorResultMessage(AppHttpCodeEnum.SERVER_ERROR,exception.getMessage());
}
}
边栏推荐
- Implementation of book borrowing management system based on C language
- 1172. The plate stack has a sequence table + stack
- WPF简单登录页面的完成案例
- 暑期总结(二)
- Vmware16 create virtual machine: cannot create a new virtual machine, do not have permission to perform this operation
- WPF nested layout case
- gin 模版
- JS chicken laying eggs and egg laying chickens. Who appeared earlier, object or function? Is function an instance of function?
- OCR光学字符识别方法汇总
- Why does ETL often become ELT or even let?
猜你喜欢
随机推荐
CMOS芯片制造全工艺流程
作业7.28 文件IO与标准IO
彻底搞懂kubernetes调度框架与插件
MySQL advanced (Advanced) SQL statement (I)
如何与斯堪尼亚SCANIA建立EDI连接?
Flink real time warehouse DWD layer (traffic domain) template code
VMware16创建虚拟机:Win11无法安装
Image noise and matrix inversion
npm install报错npm ERR Could not resolve dependency npm ERR peer
Custom events
实现改变一段文字的部分颜色效果
H3C_ Using setting default static routing priority to realize the active and standby function of export dual lines
上采样之反卷积操作
WPF simple login page completion case
计算程序运行时间 demo
JS break and continue and return keywords
论文阅读 (62):Pointer Networks
0 8 动态规划(Dynamic Programming)
【charles日常问题】开启charles,使用不了钉钉
[Charles' daily problems] when you open Charles, you can't use nails









