網站首頁 編程語言 正文
使用全局異常來避免冗余的代碼:
使用全局異常前:
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和全局異常的執行順序:
- 當發生異常時,首先會檢查當前方法中是否存在 try-catch 塊來捕獲該異常。如果存在適用的 catch 塊,則會執行 catch 塊中的代碼,并且程序將繼續執行 catch 塊之后的代碼。此時,全局異常處理器不會被觸發。
- 如果當前方法中沒有適用的 try-catch 塊來捕獲異常,或者異常在 try-catch 塊中沒有被捕獲,則異常將被傳遞給調用該方法的上層調用棧。
- 如果異常一直傳遞到了最外層的調用棧,并且沒有被任何 try-catch 塊捕獲,那么全局異常處理器將會被觸發。
原文鏈接:https://blog.csdn.net/weixin_42382291/article/details/131579300
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-12-27 go?build失敗報方法undefined的解決過程_Golang
- 2022-03-23 c++特殊構造函數詳解_C 語言
- 2023-06-03 Android廣播機制原理與開發_Android
- 2023-07-05 讓Linux中的SCP遠程復制不再需要輸入密碼
- 2022-02-07 SSH連服務器提示“Permission denied(publickey,gssapi-keyex
- 2023-02-17 pytorch/transformers?最后一層不加激活函數的原因分析_python
- 2022-09-19 Python?matplotlib數據可視化圖繪制_python
- 2022-06-15 C#實現冒泡排序和插入排序算法_C#教程
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支