一、使用web技术处理(web.xml文件加入以下代码)
<!-- 404异常-->
<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page>
<!-- 500异常-->
<error-page>
<error-code>500</error-code>
<location>/500.jsp</location>
</error-page>
二、使用SpringMVC的异常处理
1.定义一个异常处理类,类前面加注解@ControllerAdvice
2.定义异常处理方法,方法前面加注解@ExceptionHandler
@ControllerAdvice
public class ExceptionAdvice {
@ExceptionHandler(ArithmeticException.class)
public String arithmetic(Exception e){
return "error/arithmetic";
}
}