在线教育--统一异常处理

在全局模块中 创建个类 去制定全局异常类
注意相关依赖 还有R 别引错包了 不然无法执行

package com.mumu.common.config.exceptionhandler;






import com.mumu.commonutils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    //处理全局异常
    @ExceptionHandler(Exception.class)//表示只要出现了括号内中的异常 则执行此方法
    @ResponseBody//为了输出内容
    public R error(Exception e) {


        e.printStackTrace();
        return  R.error().message("执行了全局异常处理");
    }


    //处理特定异常
    @ExceptionHandler(ArithmeticException.class)//表示只要出现了括号内中ArithmeticException  的异常 则执行此方法
    @ResponseBody
    public R error(ArithmeticException e){

        e.printStackTrace();
        return  R.error().message("执行了ArithmeticException异常处理");

    }



    @ExceptionHandler(GuliException.class)//表示只要出现了括号内中GuliException  的异常 则执行此方法
    @ResponseBody
    public R error(GuliException e){
        log.error(e.getMessage());//这个方法 能让异常写到日志文件中去
        e.printStackTrace();
        return  R.error().code(e.getCode()).message(e.getMsg());

    }



}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值