一、@ExceptionHandler介绍

@ExceptionHandler注解我们一般是用来自定义异常的。
可以认为它是一个异常拦截器(处理器)。

异常间的层次关系

@ExceptionHandler详解_spring

二、@ExceptionHandler的使用

1:极简测试,一共4个类:

		1、一个SpringBoot启动类
		2、一个控制层
		3、一个异常处理类
		4、一个service类

启动类:ExceptionhandlerdemoApplication

package com.example.exceptionhandlerdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ExceptionhandlerdemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(ExceptionhandlerdemoApplication.class, args);
    }

}

异常处理类

package com.example.exceptionhandlerdemo;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    private final Logger logger = LogManager.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler({Exception.class})    //申明捕获那个异常类
    public String ExceptionDemo(Exception e) {
        logger.error(e.getMessage(), e);
        return "自定义异常返回";
    }

}

业务层:UserInfoSerimpl

package com.example.exceptionhandlerdemo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service("userInfoService")
public class UserInfoSerimpl {
    private Logger logger = LoggerFactory.getLogger(UserInfoSerimpl.class);

    public void saveUserInfo() {

        logger.error("获取用户信息失败");
        test1();
        logger.info("ddddddddd");
    }


    private void test1(){
        logger.error("test1 失败");
        throw new RuntimeException();

    }
}
测试:http://localhost:8080/yu/test
输出:自定义异常返回

2:关于ExceptionHandler定义的拦截器之间的优先级

在GlobalExceptionHandler类中定义两个拦截器

	  @ExceptionHandler({RuntimeException.class})    //申明捕获那个异常类
    public String RuntimeExceptionDemo(Exception e) {
        logger.error(e.getMessage(), e);
        return "运行时异常返回";
    }

    @ExceptionHandler({NumberFormatException.class})    //申明捕获那个异常类
    public String NumberFormatExceptionDemo(Exception e) {
        logger.error(e.getMessage(), e);
        return "数字转换异常返回";
    }

在UserInfoSerimpl的test1方法中定义一个数字转换异常,

这个异常在运行时异常之前出现。

    private void test1(){
        logger.error("test1 失败");
        	String a = "123a";
        	Integer b = Integer.valueOf(a);
        throw new RuntimeException();

    }
测试:http://localhost:8080/yu/test
输出:自定义异常返回
结论:自定义的异常越详细,得到的异常结果就越详细。

3:为什么我们不直接使用一个Exception完事

Exception什么的异常太过广泛,我们直接抛出所有异常信息,对用户而言是非常不友好的。

在事务管理里,如果我们自定义的异常继承的是Exception,则事务无效。如果我们是继承RuntimeException,则不会出现这个问题。