首页 > 其他分享 >全局异常

全局异常

时间:2022-10-21 10:36:23浏览次数:169  
标签:BusinessException jsp error msg 全局 异常 public

1. 全局异常概念

在JavaEE项目的开发中,不管是对底层的数据库操作过程,还是业务层的处理过程,还是控制层的处理过程,都不可避免会遇到各种可预知的、不可预知的异常需要处理。
每个过程都单独处理异常,系统的代码耦合度高,工作量大且不好统一,维护的工作量也很大。
SpringMvc对于异常处理这块提供了支持,通过SpringMvc提供的全局异常处理机制,能够将所有类型的异
常处理从各处理过程解耦出来,这样既保证了相关处理过程的功能较单一,也实现了 异常信息的统一处理和维护。
全局异常实现方式Spring MVC处理异常有3种方式
1.使用Spring MVC提供的简单异常处理器SimpleMappingExceptionResolver;
2.实现Spring的异常处理接口HandlerExceptionResolver自定义自己的异常处理器:
3.使用@ExceptionHandler注解实现异常处理;

2.SimpleMappingExceptionResolver

2.1 mvc.xml

<!--    全局异常-->
    <bean
        class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<!--        页面转发出现异常 设置默认错误页面-->
        <property name="defaultErrorView" value="error"></property>
<!--        错误发生时 设置错误变量名-->
        <property name="exceptionAttribute" value="ex"></property>
<!--        指定异常界面-->
        <property name="exceptionMappings">
            <props>
                <prop key="org.example.ssm.exception.BusinessException">business_error</prop>
            </props>
        </property>
    </bean>

2.2 BusinessException

public class BusinessException extends RuntimeException{
    private String msg;
    private Integer code=300;

    public BusinessException() {
        super("BusinessException");
    }

    public BusinessException(String message) {
        super(message);
        this.msg=msg;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }
}

2.3 异常位置

@RequestMapping("exe")
    public void fun(){
        if(true){
            System.out.println("BE");
            throw new BusinessException("业务异常");
        }
    }

3.HandlerExceptionResolver

GlobalExceptionResolver类

@Component
public class GlobalExceptionResolver implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        ModelAndView mv=new ModelAndView("error");
        mv.addObject("ex","default error");

        if(e instanceof BusinessException){
            BusinessException businessException=(BusinessException) e;
            mv.setViewName("business_error");
            mv.addObject("ex",businessException.getMsg());
        }

        return mv;
    }
}

4.@ExceptionHandler

BaseController类

//要继承BaseController 才可以实现
public class BaseController {

    @ExceptionHandler
    public String exc(HttpServletRequest request, HttpServletResponse response, Exception e){
        request.setAttribute("ex",e);
        if(e instanceof BusinessException){
            return "business_error";
        }

        return "error";
    }
}

business_error.jsp

<html>
<head>
    <title>Title</title>
</head>
<body>
    business_error ${ex}
</body>
</html>

5.未捕获异常

web.xml

<!--  未捕获异常-->
  <error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>jsp/500.jsp</location>
  </error-page>
  <error-page>
    <error-code>500</error-code>
    <location>jsp/500.jsp</location>
  </error-page>
  <error-page>
    <error-code>404</error-code>
    <location>jsp/404.jsp</location>
  </error-page>

标签:BusinessException,jsp,error,msg,全局,异常,public
From: https://www.cnblogs.com/lwx11111/p/16812629.html

相关文章