微商需要做网站吗,八爪鱼 wordpress,北京中小企业建站价格,阿里巴巴网站域名文章目录1#xff0c;基本使用方法2#xff0c;注解的参数3#xff0c;就近原则4#xff0c;注解方法的返回值5#xff0c;错误的操作1#xff0c;基本使用方法
Spring的ExceptionHandler可以用来统一处理方法抛出的异常#xff0c;比如这样#xff1a;
ExceptionHan…
文章目录1基本使用方法2注解的参数3就近原则4注解方法的返回值5错误的操作1基本使用方法
Spring的ExceptionHandler可以用来统一处理方法抛出的异常比如这样
ExceptionHandler()
public String handleExeption2(Exception ex) {System.out.println(抛异常了: ex);ex.printStackTrace();String resultStr 异常默认;return resultStr;
}当我们使用这个ExceptionHandler注解时我们需要定义一个异常的处理方法比如上面的handleExeption2()方法给这个方法加上ExceptionHandler注解这个方法就会处理类中其他方法被RequestMapping注解抛出的异常。
2注解的参数
ExceptionHandler注解中可以添加参数参数是某个异常类的class代表这个方法专门处理该类异常比如这样
ExceptionHandler(NumberFormatException.class)
public String handleExeption(Exception ex) {System.out.println(抛异常了: ex);ex.printStackTrace();String resultStr 异常NumberFormatException;return resultStr;
}此时注解的参数是NumberFormatException.class表示只有方法抛出NumberFormatException时才会调用该方法。
3就近原则
当异常发生时Spring会选择最接近抛出异常的处理方法。
比如之前提到的NumberFormatException这个异常有父类RuntimeExceptionRuntimeException还有父类Exception如果我们分别定义异常处理方法ExceptionHandler分别使用这三个异常作为参数比如这样
ExceptionHandler(NumberFormatException.class)
public String handleExeption(Exception ex) {System.out.println(抛异常了: ex);ex.printStackTrace();String resultStr 异常NumberFormatException;return resultStr;
}ExceptionHandler()
public String handleExeption2(Exception ex) {System.out.println(抛异常了: ex);ex.printStackTrace();String resultStr 异常默认;return resultStr;
}ExceptionHandler(RuntimeException.class)
public String handleExeption3(Exception ex) {System.out.println(抛异常了: ex);ex.printStackTrace();String resultStr 异常RuntimeException;return resultStr;
}那么当代码抛出NumberFormatException时调用的方法将是注解参数NumberFormatException.class的方法也就是handleExeption()而当代码抛出IndexOutOfBoundsException时调用的方法将是注解参数RuntimeException的方法也就是handleExeption3()。
4注解方法的返回值
标识了ExceptionHandler注解的方法返回值类型和标识了RequestMapping的方法是统一的可参见RequestMapping的说明比如默认返回Spring的ModelAndView对象也可以返回String这时的String是ModelAndView的路径而不是字符串本身。
有些情况下我们会给标识了RequestMapping的方法添加**ResponseBody**比如使用Ajax的场景直接返回字符串异常处理类也可以如此操作添加ResponseBody注解后可以直接返回字符串比如这样
ExceptionHandler(NumberFormatException.class)
ResponseBody
public String handleExeption(Exception ex) {System.out.println(抛异常了: ex);ex.printStackTrace();String resultStr 异常NumberFormatException;return resultStr;
}这样的操作可以在执行完方法后直接返回字符串本身。
5错误的操作
使用ExceptionHandler时尽量不要使用相同的注解参数。
如果我们定义两个处理相同异常的处理方法
ExceptionHandler(NumberFormatException.class)
ResponseBody
public String handleExeption(Exception ex) {System.out.println(抛异常了: ex);ex.printStackTrace();String resultStr 异常NumberFormatException;return resultStr;
}ExceptionHandler(NumberFormatException.class)
ResponseBody
public String handleExeption2(Exception ex) {System.out.println(抛异常了: ex);ex.printStackTrace();String resultStr 异常默认;return resultStr;
}两个方法都处理NumberFormatException这种定义方式编译可以通过而当NumberFormatException真正被抛出时Spring会给我们报错
java.lang.IllegalStateException: Ambiguous ExceptionHandler method mapped for [class java.lang.NumberFormatException]: {public java.lang.String TestController.handleExeption(java.lang.Exception), public java.lang.String TestController.handleExeption2(java.lang.Exception)}at org.springframework.web.method.annotation.ExceptionHandlerMethodResolver.addExceptionMapping(ExceptionHandlerMethodResolver.java:102) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]at org.springframework.web.method.annotation.ExceptionHandlerMethodResolver.init(ExceptionHandlerMethodResolver.java:66) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]