自定义异常及异常全局处理

版权声明:分享知识是一种快乐,愿你我都能共同成长! https://blog.csdn.net/qidasheng2012/article/details/84550539

一、Java异常分类

这里写图片描述

java中异常均继承自Throwable,其有两个重要的直接子类error与exception

  1. Error:

大部分是由虚拟机报出来的错误,是程序无法处理的错误,如 OutOfMemoryError,当JVM需要更多内存空间而得不到满足时,就会报出OutOfMemoryError(内存溢出)

  1. Exception
  • 非RuntimeException(编译期异常)

    在代码书写时,编译器给你检查提示你要进行try catch或throws处理。如IOException,SQLException等

  • RuntimeException(运行时异常)
    编译器不会帮你自动检查,当你运行程序时,虚拟机才会给你爆出错误让你去处理,这个往往是我们编码逻辑或不规范导致的。

二、案例分析
1、自定义的业务异常类:BusinessException

@Getter
public class BusinessException extends Exception {

    private int code;

    public BusinessException(int code,String message) {
        super(message);
        this.code = code;
    }

}

2、在service层中抛出业务性异常:

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public void testException() throws Exception {
        throw new BusinessException(10001,"参数异常");
    }
}

3、在controller层中继续向上抛异常

@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;

    @RequestMapping("/testException")
    public void testException() throws Exception {
        studentService.testException();
    }
}

4、异常统一拦截

返回的异常,都需要经过统一的处理,同样spring也提供了接口对异常的返回
全局异常处理类:GlobalExceptionHandler

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public BaseResponse jsonErrorHandler(HttpServletRequest request, Exception e) throws Exception{
        BaseResponse response = new BaseResponse();
        
        if( e instanceof BusinessException ){
            response.setCode(((BusinessException)e).getCode());
            response.setMessage(e.getMessage());
        } else {
            log.error(e.getMessage(),e);
            response.setCode(ResponseEnum.SYSTEM_ERROR.getCode());
            response.setMessage(ResponseEnum.SYSTEM_ERROR.getMessage());
        }
        
        return response;
    }
}

@ControllerAdvice:
是一个@Component,用于定义@ExceptionHandler,@InitBinder和@ModelAttribute方法,适用于所有使用@RequestMapping方法。

BaseResponse 基础响应实体类

import lombok.Data;

@Data
public class BaseResponse {
    private Integer code;
    private String message;
}

ResponseEnum

@Getter
@AllArgsConstructor
public enum ResponseEnum {
    SYSTEM_ERROR(9999,"系统异常");
    
    private Integer code;
    private String message;
}

三、测试
访问: http://localhost:8080/testException
在这里插入图片描述
大功告成,和预期的结果一样

四、总结
处理逻辑:
自定义异常类 --> 在service层向上抛出自定义异常类 --> 在controller层把自定义异常类继续向上抛 --> 用自定义的全局异常处理类进行统一拦截捕获并处理

自定义异常的好处:
1.异常信息明确,能够快速定位问题
2.统一代码业务性问题处理规范
3.方便错误数据的封装,后台数据机构的统一(统一异常拦截体现)

猜你喜欢

转载自blog.csdn.net/qidasheng2012/article/details/84550539