[四]SpringBoot 之 捕捉全局异常

在class注解上@ControllerAdvice,

在方法上注解上@ExceptionHandler(value = Exception.class),具体代码如下:

package me.shijunjie.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    public void handleGlobalException(HttpServletRequest req, Exception e) {
        //打印异常信息:
        //e.printStackTrace();
        System.out.println("GlobalExceptionHandler.handleGlobalException()");
    }

}
@RequestMapping("/zeroException")
    public int zeroException(){
       return 111/0;
    }
原文地址:https://www.cnblogs.com/s648667069/p/6398696.html