SpringMVC异常处理机制

SpringMVC异常处理机制

1. 异常处理的思路

1.系统中异常包括两类:预期异常运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发、测试等手段减少运行时异常的发生。

2.系统的Dao、Service、Controller出现都通过throws Exception向上抛出,最后由SpringMVC前端控制器交由异常处理器进行异常处理,如下图
在这里插入图片描述

2. 异常处理两种方式

  • 使用Spring MVC提供的简单异常处理器SimpleMappingExceptionResolver

  • 实现Spring的异常处理接口HandlerExceptionResolver 自定义自己的异常处理器

制造异常的方法 ExceptionServiceImpl.java

public class ExceptionServiceImpl implements ExceptionService {
    @Override
    public void show1() {
        System.out.println("抛出类型转换异常...");
        Object str = "zhangsan";
        Integer num = (Integer)str;
    }

    @Override
    public void show4() throws MyException {
        System.out.println("抛出自定义异常...");
        throw new MyException();
    }
}

还可以制造其他异常,如下

public void show2(){
    System.out.println("抛出除0异常...");
    int i=1/0;
}
public void show3() throw FileNotFoundException {
    System.out.println("文件找不到异常...");
    InputStream in = new FileInputStream("C:/xxx/xxx/xxx");
}

自定义异常,为了演示,里面暂时为空

public class MyException extends Exception{
}

3. 简单异常处理器SimpleMappingExceptionResolver

SpringMVC已经定义好了该类型转换器,在使用时可以根据项目情况进行相应异常与视图的映射配置

<!--配置简单映射异常处理器-->
<bean 
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<!--<property name="defaultErrorView" value="error1"/> -->
<property name="exceptionMappings">
<map> 
<!-- 自定义的异常MyException-->
<entry key="com.itheima.exception.MyException" value="error2"/>
<entry key="java.lang.ClassCastException" value="error3"/>
</map>
</property>
</bean>

error1.jsp error2.jsp error3.jsp 均是自定义的异常显示界面

在这里插入图片描述

4. 自定义异常处理步骤

① 创建异常处理器类实现HandlerExceptionResolver

public class MyExceptionResolver implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest request, 
HttpServletResponse response, Object handler, Exception ex) {
//处理异常的代码实现
//创建ModelAndView对象
    ModelAndView modelAndView = new ModelAndView();
    if(e instanceof MyException){
        modelAndView.addObject("info","自定义异常");
    }else if(e instanceof ClassCastException){
        modelAndView.addObject("info","类型转换异常");
    }  
    modelAndView.setViewName("error");
    return modelAndView;
}
}

② 配置异常处理器

<bean id="exceptionResolver" 
class="com.itheima.exception.MyExceptionResolver"/>

③ 编写异常页面error.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
这是一个最终异常的显示页面<br>
    <h3>
        ${info}
    </h3>
    
</body>
</html>

④ 测试异常跳转

@RequestMapping("/show")
public String show() throws MyException {
	demoService.show1();
   //demoService.show4();
    return "index";
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白衣卿巷

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值