Spring exception handler

1. Anomaly analysis

 Question:
  Programs may inevitably generate exceptions at all levels. How should we deal with these exceptions? If we just use the try... catch... statement alone in the method to get a It is undoubtedly unrealistic to capture and process one by one, because the number of exceptions is very large and the types of exceptions are unpredictable, so we can use the exception handler provided by the Spring framework for processing.

 Solution: Use exception handlers for unified processing in the presentation layer (exceptions are called layer by layer through the presentation layer, and will eventually be thrown back to the presentation layer, so they can generally be written in the presentation layer for processing)

2. Exception handler

2.1 Exception handler core

@ControllerAdvice: 声明其为异常处理器类, 用来处理系统当中的异常
@ResponseBody: 声明其返回数据放在返回体当中(用于返回异常处理之后的数据交给前端)
@RestControllerAdvice = @ControllerAdvice + @ResponseBody

关键: 在每个异常处理方法的上面需要使用对应注解声明其处理异常的种类
@ExceptionHandler(xxx.class)
@ControllerAdvice // 注意此类需要让Spring配置类扫描
@ResponseBody
public class GlobalExceptionHandler {
    
    

	@ExceptionHandler(Exception.class) // 处理全部异常
	public String doException(Exception ex){
    
     // 用形参来接收异常
		/*
			1. 记录日志(错误堆栈)
			2. 发送邮件 -- 开发人员
			3. 发送邮件 -- 运维人员
		 */
		System.out.println(ex.getMessage());
		return "发生了Exception类型的异常.....";
	}
}

  Controller layer

@RestController
public class AController {
    
    

	@RequestMapping("/TestA")
	public String TestA(){
    
    
		System.out.println("This is Test A");
		int x = 1 / 0;
		return "Successful connect TestA .......";
	}
}

  process result
Insert image description here

2.2 Exception handling sequence

  If the exception handling has an inclusion relationship, it will give priority to matching more accurate processors (that is, give priority to subclasses, and if the subclasses do not match, then gradually match them upwards)

@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
    
    
	@ExceptionHandler(Exception.class) // 处理全部异常
	public String doException(Exception ex){
    
     // 用形参来接收异常
		System.out.println(ex.getMessage());
		System.out.println("发生了Exception类型的异常.....");
		return "发生了Exception类型的异常.....";
	}

	@ExceptionHandler(ArithmeticException.class) // 处理算术异常
	public String doZeroException(ArithmeticException ex){
    
     // 用形参来接收异常
		System.out.println(ex.getMessage());
		System.out.println("发生了ArithmeticException类型的异常.....");
		return "发生了ArithmeticException类型的异常.....";
	}
}

Insert image description here

3. Custom exceptions

  Users can respond to specific exceptions according to their own project settings and then handle them.

public class MyException extends Exception{
    
    
	Integer id;
	public MyException(Integer id, String message){
    
    
		super(message); // 利用父类注明异常信息
		this.id = id;
	}

	@Override
	public String toString() {
    
    
		return "异常的ID为:" + id + "\n" +
				"异常的详细信息为" + getMessage();
	}
}

@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
    
    
	@ExceptionHandler(MyException.class) // 处理自定义异常
	public String MyException(MyException ex){
    
    
		System.out.println(ex.getMessage());
		return "自定义异常";
	}

}

@RestController
public class AController {
    
    

	@RequestMapping("/TestA")
	public String TestA() throws MyException {
    
    
		System.out.println("This is Test A");
		throw new MyException(101, "101号自定义异常.....");
	}
}

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_51566349/article/details/131591767