SpringMVC统一异常处理

/**
 * @author wen.jie
 * @Classname GlobalExceptionHandler
 * @Description 统一异常处理
 * @Date 2020/6/27
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = {ArithmeticException.class})
    public Object handlerException(Exception e){
        log.info(Arrays.toString(e.getStackTrace()));
        return new CommonResult().failed("数学算数异常");
    }

    @ExceptionHandler(value = {NullPointerException.class})
    public Object handlerException2(Exception e){
        log.info(Arrays.toString(e.getStackTrace()));
        return new CommonResult().failed("空指针异常");
    }

    @ExceptionHandler(value = {IndexOutOfBoundsException.class})
    public Object handlerException3(Exception e){
        log.info(Arrays.toString(e.getStackTrace()));
        return new CommonResult().failed("下标越界异常");
    }
}

  

原文地址:https://www.cnblogs.com/wwjj4811/p/13199964.html