日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

springboot優雅處理異常

作者:釋然狗 更新時間: 2023-07-07 編程語言

使用全局異常來避免冗余的代碼:

使用全局異常前:

controller層通篇的try-catch以及大量重復的代碼來捕獲在service層拋出的異常

使用全局異常后:

只需要增加一個全局異常類,里面設置好要捕獲的異常以及處理方式,就可以了,針對不通用的異常處理可以使用try-catch

全局異常設置:

package cn.test.account.handler;

import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import cn.test.account.domain.ReturnCode;
import cn.test.account.exceptions.BusinessException;
import cn.test.account.model.response.CodeResponse;

/**
 * 全局異常類,處理未被捕獲的異常
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);


    @ExceptionHandler(BusinessException.class)
    public ResponseEntity<?> handleBusinessException(BusinessException ex) {
        // 處理自定義異常并返回自定義響應
        CodeResponse idResponse = new CodeResponse();
        idResponse.setCode(ex.getCode());
        idResponse.setMessage(ex.getMessage());
        return ResponseEntity.ok().body(idResponse);
    }


    @ExceptionHandler(Exception.class)
    public ResponseEntity<?> handleException(Exception ex) {
        logger.error("runtimeException:", ex);
        CodeResponse idResponse = new CodeResponse();
        idResponse.setCode(ReturnCode.SYSTEM_IDENTIFY);
        idResponse.setMessage(ex.getMessage() != null ? ex.getMessage() : "system error");
        return ResponseEntity.ok().body(idResponse);
    }

}

其中BusinessException 是自定義異常,如下:

package cn.test.account.exceptions;

/**
 * @author aaa
 * @version 1.0.0
 * @create 2023-03-21 10:58
 */
public class BusinessException  extends BaseException{

    Integer code;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public BusinessException() {
        super();
    }

    public BusinessException(String message, Throwable cause) {
        super(message, cause);
    }

    public BusinessException(String message) {
        super(message);
    }

    public BusinessException(Throwable cause) {
        super(cause);
    }

    public BusinessException(String message, Integer code,Throwable cause) {
        super(message, cause);
        this.code = code;
    }

    public BusinessException(String message, Integer code) {
        super(message);
        this.code = code;
    }
}
package cn.test.account.exceptions;

/**
 * 異常基類
 * @description: 異常基類
 * @author aaa
 * @date 2022/9/27 11:00
 * @version 1.0
 */
public class BaseException extends RuntimeException {
    public BaseException() {
        super();
    }

    public BaseException(String message, Throwable cause) {
        super(message, cause);
    }

    public BaseException(String message) {
        super(message);
    }

    public BaseException(Throwable cause) {
        super(cause);
    }
}

疑惑點:那么當有一個異常被捕獲(即try-catch)后,會走全局異常還是catch中的邏輯呢?

try catch和全局異常的執行順序:

  1. 當發生異常時,首先會檢查當前方法中是否存在 try-catch 塊來捕獲該異常。如果存在適用的 catch 塊,則會執行 catch 塊中的代碼,并且程序將繼續執行 catch 塊之后的代碼。此時,全局異常處理器不會被觸發。
  2. 如果當前方法中沒有適用的 try-catch 塊來捕獲異常,或者異常在 try-catch 塊中沒有被捕獲,則異常將被傳遞給調用該方法的上層調用棧。
  3. 如果異常一直傳遞到了最外層的調用棧,并且沒有被任何 try-catch 塊捕獲,那么全局異常處理器將會被觸發。

原文鏈接:https://blog.csdn.net/weixin_42382291/article/details/131579300

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新