@ExceptionHandler详解

1:@ExceptionHandler介绍

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

异常间的层次关系
异常间的层次关系

2: @ExceptionHandler的使用

一:极简测试,一共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 "自定义异常返回";
    }

}

控制层TestControll

package com.example.exceptionhandlerdemo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/yu")
public class TestControll {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private UserInfoSerimpl userInfoSerimpl;

    @ResponseBody
    @RequestMapping("/test")
    public String test(){
        logger.info("11111111111");
        userInfoSerimpl.saveUserInfo();

        logger.info("2222222222");
        return "sdfsfs";
    }
}

业务层: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
输出:自定义异常返回

二:关于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
输出:自定义异常返回
结论:
	自定义的异常越详细,得到的异常结果就越详细。

三:为什么我们不直接使用一个Exception完事
1:Exception什么的异常太过广泛,我们直接抛出所有异常信息,对用户而言是非常不友好的。
2:在事务管理里,如果我们自定义的异常继承的是Exception,
则事务无效。如果我们是继承RuntimeException,则不会
出现这个问题。

  • 41
    点赞
  • 153
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值