網站首頁 編程語言 正文
前言
與defer類似的是,goroutine 中也有一個_panic鏈表頭指針指向一個_panic鏈,發生panic的時候也是在鏈表頭插入_panic結構體(執行gopanic)
在執行過程中發生了panic。那么panic以后的代碼不會執行,轉而執行panic的邏輯,再執行defer,執行到的defer要將started標記為true,同時將其defer結構體中的_panic指針指向當前的_panic,表示這個defer是由該panic觸發的。再去執行defer鏈表,如果defer執行中還觸發了panic,panic后的代碼不載執行,將這個panic插入panic鏈頭,同時將其作為當前panic。當遇到了與當前panic不符的defer,就找到該defer上的panic,將其標記為已終止,從defer鏈表中移除當前執行的defer。打印panic移除信息,從鏈表尾開始逐步輸出
流程
panic執行defer的流程:
- 先標記
started=true,_panic=&panic
- 后釋放
- 目的是為了終止之前發生的panic
異常信息的輸出方式:
- 所有還在panic鏈表上的項會被輸出
- 順序與發生panic的順序一致
// A _panic holds information about an active panic. // // A _panic value must only ever live on the stack. // // The argp and link fields are stack pointers, but don't need special // handling during stack growth: because they are pointer-typed and // _panic values only live on the stack, regular stack pointer // adjustment takes care of them. type _panic struct { // argp 存儲當前要執行的defer的函數參數地址 argp unsafe.Pointer // pointer to arguments of deferred call run during panic; cannot move - known to liblink // arg panic函數自己的參數 arg interface{} // argument to panic // link,鏈到之前發生的panic link *_panic // link to earlier panic pc uintptr // where to return to in runtime if this panic is bypassed sp unsafe.Pointer // where to return to in runtime if this panic is bypassed // recovered 標識panic是否被恢復 recovered bool // whether this panic is over // aborted 標識panic是否被終止 aborted bool // the panic was aborted goexit bool }
關于recover
recover只執行一件事
- 將當前執行的panic的recovered字段置為true
在每個defer執行完以后panic處理流程都會檢查當前panic是否被recover
- 如果當前panic已經被恢復,就會將它從panic鏈中移除
- 執行到的defer也會被移除,同時要保存_defer.sp和_defer.pc
利用_defer.sp和_defer.pc跳出當前panic的處理流程,通過棧指針判斷,只執行當前函數中注冊的defer函數
在發生recover的函數正常結束后才會進入到檢測panic是否被恢復的流程
當recover的函數又發生panic時,goroutine會將該panic加入到鏈頭,設置為當前panic,再去執行defer鏈表,發現當前defer是當前panic執行的,移除當前defer,繼續執行下一個,直到發現不是當前panic執行的,在panic鏈上找到那個panic,輸出異常信息
對于已經recover標記的panic在輸出異常信息時會加上recovered標記
原文鏈接:https://juejin.cn/post/7102402219504320543
相關推薦
- 2022-04-28 Go語言錯誤處理異常捕獲+異常拋出_Golang
- 2023-04-06 C++中的memset用法詳解_C 語言
- 2022-10-30 C++解析wav文件方法介紹_C 語言
- 2023-01-20 Python輸入圓半徑,計算圓周長和面積的實現方式_python
- 2022-09-08 pandas時間序列之如何將int轉換成datetime格式_python
- 2022-09-04 react?表單數據形式配置化設計_React
- 2022-05-25 Windows系統修改Jenkins端口號_win服務器
- 2022-09-26 Ajax異步請求的五個步驟及實戰案例_AJAX相關
- 最近更新
-
- 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同步修改后的遠程分支