13.1、异常处理——HandlerExceptionResolver 和@ExceptionHandler

来源:互联网 发布:阿里云直播 摄像机 编辑:程序博客网 时间:2024/06/05 18:01

Spring MVC通过HandlerExceptionResolver处理程序的异常,包括Handler的映射、数据绑定以及目标方法的执行。HandlerExceptionResolver时一个接口,该接口的实现类都有处理异常的功能。HandlerExceptionResolver是该接口应用广泛的一个实现类,并且DispatcherServlet默认装配了HandlerExceptionResolver 的Bean。使用HandlerExceptionResolver处理异常时,要在spring配置文件中配置。
示例如下:
1、web.xml和spring的配置文件
web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns="http://xmlns.jcp.org/xml/ns/javaee"    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"    id="WebApp_ID" version="3.1">    <!-- The front controller of this Spring Web application, responsible for handling all application requests -->    <servlet>        <servlet-name>springDispatcherServlet</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:springmvc.xml</param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>    <!-- Map all requests to the DispatcherServlet for handling -->    <servlet-mapping>        <servlet-name>springDispatcherServlet</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping></web-app>

spring的配置文件springmvc.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:aop="http://www.springframework.org/schema/aop"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:mvc="http://www.springframework.org/schema/mvc"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">    <context:component-scan base-package="com.lzj.springmvc"></context:component-scan>    <!--配置视图解析器-->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/WEB-INF/views/"></property>        <property name="suffix" value=".jsp"></property>    </bean>    <mvc:annotation-driven></mvc:annotation-driven></beans>

2、创建请求
在WebContent下面创建index.jsp,内容为:

<a href="springMVC/testExceptionHandlerExceptionResolver?i=0">Test ExceptionHandlerExceptionResolver</a>

3、创建请求控制器
控制器用于截获index.jsp中的请求

@Controller@RequestMapping("/springMVC")public class TestSpringMVC {    @RequestMapping("/testExceptionHandlerExceptionResolver")    /*请求中的i的值为0,当控制器获取该值,执行10/0时出现异常,导致程序失败*/    public String testExceptionHandlerExceptionResolver(@RequestParam("i") int i){        System.out.println("result: " + (10/i));        return "success";    }}

当执行index.jsp中请求后,控制器截获请求,并获取请求中的请求参数i,由于i的值为0,控制器中的逻辑为10/0,运行时会抛出一个异常ArithmeticException

4、处理异常
在TestSpringMVC控制器类中添加处理异常的方法

public class TestSpringMVC {    //ExceptionHandler注解表示处理异常的类型    @ExceptionHandler({ArithmeticException.class})    public String handleArithmeticException(Exception exception){        System.out.println("异常: " + exception);        return "success";    }    @RequestMapping("/testExceptionHandlerExceptionResolver")    public String testExceptionHandlerExceptionResolver(@RequestParam("i") int i){        System.out.println("testExceptionHandlerExceptionResolver");        System.out.println("result: " + (10/i));        return "success";    }

当index.jsp中发送请求后, testExceptionHandlerExceptionResolver控制器截获请求,并获取请求中i值后,在执行到System.out.println(“result: ” + (10/i));,出现异常,程序不在往下执行,然后handleArithmeticException处理器捕获ArithmeticException异常,处理异常程序。
Console中输出内容如下:

testExceptionHandlerExceptionResolver异常: java.lang.ArithmeticException: / by zero

5、处理异常优先级
控制器中处理异常的方法改为如下

@Controller@RequestMapping("/springMVC")public class TestSpringMVC {    @ExceptionHandler({RuntimeException.class})    public String handleArithmeticException2(Exception exception){        System.out.println("handleArithmeticException2");        System.out.println("异常: " + exception);        return "success";    }    @RequestMapping("/testExceptionHandlerExceptionResolver")    public String testExceptionHandlerExceptionResolver(@RequestParam("i") int i){        System.out.println("testExceptionHandlerExceptionResolver");        System.out.println("result: " + (10/i));        return "success";    }}

当发送index.jsp中请求后,testExceptionHandlerExceptionResolver控制器方法截获该请求,并获取请求参数i后,执行逻辑出现异常,抛出ArithmeticException异常,异常被RuntimeException类型的异常捕获,handleArithmeticException2异常处理该异常。因为ArithmeticException异常继承自RuntimeException,所以RuntimeException类型的异常可以捕获ArithmeticException异常。Console中输出内容如下:

testExceptionHandlerExceptionResolverhandleArithmeticException2异常: java.lang.ArithmeticException: / by zero

但是当程序中同时出现了RuntimeException和ArithmeticException异常处理程序,执行顺序是什么呢?
处理异常发方法如下:

@Controller@RequestMapping("/springMVC")public class TestSpringMVC {    @ExceptionHandler({RuntimeException.class})    public String handleArithmeticException2(Exception exception){        System.out.println("handleArithmeticException2");        System.out.println("异常: " + exception);        return "success";    }    @ExceptionHandler({ArithmeticException.class})    public String handleArithmeticException(Exception exception){        System.out.println("异常: " + exception);        return "success";    }    @RequestMapping("/testExceptionHandlerExceptionResolver")    public String testExceptionHandlerExceptionResolver(@RequestParam("i") int i){        System.out.println("testExceptionHandlerExceptionResolver");        System.out.println("result: " + (10/i));        return "success";    }}

发送index.jsp中的请求后,testExceptionHandlerExceptionResolver控制器方法截获请求,并获取请求中的参数i值0,控制器方法出现异常,异常先被handleArithmeticException方法捕获执行,所以异常会先被标记最底层实现类的那个异常程序处理,如果没有的话,一级一级的往上抛。
Console中输出内容如下:

testExceptionHandlerExceptionResolver异常: java.lang.ArithmeticException: / by zero

注意:如果把异常处理程序和控制器处理方法写在同一个类中,那么异常处理器只能处理该类中抛出的异常。如果想让异常处理程序能处理项目下所有类抛出的异常,可以在src下新建一个包,专门放置处理异常的异常。

阅读全文
0 0
原创粉丝点击