SpringMVC: exception handling

Exception handling

There are two types of exceptions in the system: expected exceptions and runtime exceptions. The former captures exception information by capturing exceptions, and the latter mainly reduces the occurrence of runtime exceptions by standardizing code development and testing. The dao, service, and controller of the system are thrown upward through throws Exception, and finally the springmvc front-end controller is handed over to the exception processor for exception handling, as shown below:

springmvc provides a global exception handler (a system has only one exception handler) for unified exception handling.

Custom exception class

Define exception classes for different exception types and inherit Exception.

public class CustomException extends Exception {
	
	//异常信息
	public String message;
	
	public CustomException(String message){
		super(message);
		this.message = message;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
}

Global exception handler

Idea: When the system encounters an exception, it is manually thrown in the program, dao is thrown to service, service is to controller, and controller is thrown to the front-end controller, and the front-end controller calls the global exception handler.

Global exception handler handling ideas:

  1. Parse out the exception type
  2. If the exception type is a system-defined exception, the exception information is directly taken out and displayed on the error page
  3. If the exception type is not a system-defined exception, construct a custom exception type (the message is "Unknown Error") 
public class CustomExceptionResolver implements HandlerExceptionResolver {
    
    @Override
	public ModelAndView resolveException(HttpServletRequest request,HttpServletResponse response, Object handler, Exception ex) {
			
		CustomException customException = null;
		
		if(ex instanceof CustomException){
			customException = (CustomException)ex;
		}else{
			customException = new CustomException("未知错误");
		}
		
		//错误信息
		String message = customException.getMessage();
		ModelAndView modelAndView = new ModelAndView();
		//将错误信息传到页面
		modelAndView.addObject("message", message);
		//指向错误页面
		modelAndView.setViewName("error");
		
		return modelAndView;
	}
}

error.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>错误提示</title>
</head>
<body>
    ${message }
</body>
</html>

Configure global exception handler in springmvc.xml

<bean class="cn.itcast.ssm.exception.CustomExceptionResolver "></bean>

An exception needs to be thrown manually in any of controller, service, and dao.

If the exception is manually thrown in the program, the customized exception information is displayed in the error page. If it is not manually thrown, it means a runtime exception, and only "Unknown Error" is displayed on the error page.

If there are exceptions related to business functions, it is recommended to throw exceptions in the service.

Exceptions not related to business functions are recommended to be thrown in the controller.

Published 202 original articles · praised 37 · 30,000+ views

Guess you like

Origin blog.csdn.net/lovecuidong/article/details/103958372