這篇文章主要介紹“Golang中panic與recover的區別是什么”,在日常操作中,相信很多人在Golang中panic與recover的區別是什么問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Golang中panic與recover的區別是什么”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
與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只執行一件事
將當前執行的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標記
到此,關于“Golang中panic與recover的區別是什么”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。