Throwable
Error
OutOfMemoryError(OOM)
Exception
RuntimeException
NullPointerException:某个为null的对象调用了属性或方法
ClassCastException:强制转换为不匹配的数据类型
ClassNotFoundException:尝试加载的类不存在
IndexOutOfBoundsException:使用List集合时使用了越界的索引
ArrayIndexOutOfBoundsException:使用Array时使用了越界的索引
SQLException:数据库相关异常
IOException:输入输出(读写)异常
FileNotFoundException:文件找不到
NullPointerException
进行处理,那么,无论是项目的哪个环节出现该异常,都会自动按照配置的方式进行处理,而不用每个方法中逐一编写相关代码。DAY07-SpringMVC-Exception
,设计请求路径:
http://SERVER:PORT/PROJECT/ex1.do
http://SERVER:PORT/PROJECT/ex2.do
ex1.jsp
、ex2.jsp
页面显示。SimpleMappingExceptionResolver
类,用于配置异常与View组件的映射关系,如果确定某种异常出现后都会显示某个View组件,则在Spring的配置文件中:
<bean class="xx.xx.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="异常类的全名">View组件名</prop>
<prop key="异常类的全名">View组件名</prop>
<prop key="异常类的全名">View组件名</prop>
<props>
</property>
</bean>
注意:使用SimpleMappingExceptionResolver处理异常时,不可以使用@ExceptionHandler!
@ExceptionHandler
,与处理请求的方法类似,可以按需添加方法的参数,需要注意的,必须有Exception
参数: @ExceptionHandler
public String handleException(
HttpServletRequest request,
Exception ex) {
System.out.println(ex.getClass());
if (ex instanceof NullPointerException) {
return "error1";
} else if (ex instanceof ArrayIndexOutOfBoundsException) {
return "error2";
} else {
return "error3";
}
}
@RequestMapping
注解,还有@RequestParam
注解;ModelMap
);Interceptor
;原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。