十一、Exception异常处理(三种方式)

1、统一异常处理

(1)新建异常处理类

package com.chifeng.klqq.exception;

import com.chifeng.klqq.result.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Description: // 异常处理类
 * @Date: 2022-09-21 11:04
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    
    

    /**
     * 1、全局异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e){
    
    
        e.printStackTrace();
        return Result.fail(null).message("执行了全局异常处理!");
    }
}

(2)手动编写异常

/**
  * 1、教师信息:查询所有数据
  * @return
  */
 @ApiOperation("教师信息:查询所有数据")
 @GetMapping("findAll")
 public Result findAll() {
    
    
     // 新增异常
     int i = 10/0;
     List<Teacher> teacherList = teacherService.list();
     return Result.ok(teacherList).message("教师信息:查询所有数据成功!");
 }

(3)执行结果

在这里插入图片描述

2、特定异常处理

(1)异常处理类新增“特定异常处理”方法

/**
  * 2、特定异常处理
  * @param e
  * @return
  */
 @ExceptionHandler(ArithmeticException.class)
 @ResponseBody
 public Result error(ArithmeticException e){
    
    
     e.printStackTrace();
     return Result.fail(null).message("执行了ArithmeticException异常处理!");
    }

(2)手动编写异常

同“统一异常处理”一致。

(3)执行结果

在这里插入图片描述

3、自定义异常处理

(1)异常处理类新增“自定义异常”方法

/**
  * 3、自定义异常处理
  * @param e
  * @return
  */
 @ExceptionHandler(OtherException.class)
 @ResponseBody
 public Result error(OtherException e) {
    
    
     e.printStackTrace();
     return Result.fail(null).code(e.getCode()).message(e.getMsg());
 }

(2)新增“自定义异常处理类”

package com.chifeng.klqq.exception;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Description: // 自定义异常处理类
 * @Date: 2022-09-21 15:39
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class OtherException extends RuntimeException{
    
    
    private Integer code;
    private String msg;
}

(3)手动编写异常

自定义异常不会自动处理,需要try{}catch{}抛出

/**
  * 1、教师信息:查询所有数据
  * @return
  */
 @ApiOperation("教师信息:查询所有数据")
 @GetMapping("findAll")
 public Result findAll() {
    
    
     try {
    
    
         int i = 10/0;
     } catch (Exception e){
    
    
         throw new OtherException(999,"执行自定义异常处理OtherException");
     }

     List<Teacher> teacherList = teacherService.list();
     return Result.ok(teacherList).message("教师信息:查询所有数据成功!");
 }

(4)执行结果

在这里插入图片描述

4、总结

当三种异常处理方法同时存在时,去处理同一个异常。顺序:
自定义异常处理>特定异常处理>统一异常处理

代码如下:

package com.chifeng.klqq.exception;

import com.chifeng.klqq.result.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Description: // 异常处理类
 * @Date: 2022-09-21 11:04
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    
    

    /**
     * 1、全局异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e){
    
    
        e.printStackTrace();
        return Result.fail(null).message("执行了全局异常处理!");
    }

    /**
     * 2、特定异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public Result error(ArithmeticException e){
    
    
        e.printStackTrace();
        return Result.fail(null).message("执行了ArithmeticException异常处理!");
    }

    /**
     * 3、自定义异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(OtherException.class)
    @ResponseBody
    public Result error(OtherException e) {
    
    
        e.printStackTrace();
        return Result.fail(null).code(e.getCode()).message(e.getMsg());
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_61470350/article/details/126974624