Golang中panic與recover的區(qū)別
前言
與defer類似的是,goroutine 中也有一個_panic鏈表頭指針指向一個_panic鏈,發(fā)生panic的時候也是在鏈表頭插入_panic結(jié)構(gòu)體(執(zhí)行g(shù)opanic)
在執(zhí)行過程中發(fā)生了panic。那么panic以后的代碼不會執(zhí)行,轉(zhuǎn)而執(zhí)行panic的邏輯,再執(zhí)行defer,執(zhí)行到的defer要將started標記為true,同時將其defer結(jié)構(gòu)體中的_panic指針指向當前的_panic,表示這個defer是由該panic觸發(fā)的。再去執(zhí)行defer鏈表,如果defer執(zhí)行中還觸發(fā)了panic,panic后的代碼不載執(zhí)行,將這個panic插入panic鏈頭,同時將其作為當前panic。當遇到了與當前panic不符的defer,就找到該defer上的panic,將其標記為已終止,從defer鏈表中移除當前執(zhí)行的defer。打印panic移除信息,從鏈表尾開始逐步輸出
流程
panic執(zhí)行defer的流程:
- 先標記
started=true,_panic=&panic
- 后釋放
- 目的是為了終止之前發(fā)生的panic
異常信息的輸出方式:
- 所有還在panic鏈表上的項會被輸出
- 順序與發(fā)生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 存儲當前要執(zhí)行的defer的函數(shù)參數(shù)地址 argp unsafe.Pointer // pointer to arguments of deferred call run during panic; cannot move - known to liblink // arg panic函數(shù)自己的參數(shù) arg interface{} // argument to panic // link,鏈到之前發(fā)生的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是否被恢復(fù) recovered bool // whether this panic is over // aborted 標識panic是否被終止 aborted bool // the panic was aborted goexit bool }
關(guān)于recover
recover只執(zhí)行一件事
- 將當前執(zhí)行的panic的recovered字段置為true
在每個defer執(zhí)行完以后panic處理流程都會檢查當前panic是否被recover
- 如果當前panic已經(jīng)被恢復(fù),就會將它從panic鏈中移除
- 執(zhí)行到的defer也會被移除,同時要保存_defer.sp和_defer.pc
利用_defer.sp和_defer.pc跳出當前panic的處理流程,通過棧指針判斷,只執(zhí)行當前函數(shù)中注冊的defer函數(shù)
在發(fā)生recover的函數(shù)正常結(jié)束后才會進入到檢測panic是否被恢復(fù)的流程
當recover的函數(shù)又發(fā)生panic時,goroutine會將該panic加入到鏈頭,設(shè)置為當前panic,再去執(zhí)行defer鏈表,發(fā)現(xiàn)當前defer是當前panic執(zhí)行的,移除當前defer,繼續(xù)執(zhí)行下一個,直到發(fā)現(xiàn)不是當前panic執(zhí)行的,在panic鏈上找到那個panic,輸出異常信息
對于已經(jīng)recover標記的panic在輸出異常信息時會加上recovered標記
到此這篇關(guān)于Golang中panic與recover的區(qū)別的文章就介紹到這了,更多相關(guān)Go panic recover內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
golang結(jié)構(gòu)化日志slog的用法簡介
日志是任何軟件的重要組成部分,Go?提供了一個內(nèi)置日志包(slog),在本文中,小編將簡單介紹一下slog包的功能以及如何在?Go?應(yīng)用程序中使用它,感興趣的可以了解下2023-09-09Go事務(wù)中止時是否真的結(jié)束事務(wù)解析
這篇文章主要為大家介紹了Go事務(wù)中止時是否真的結(jié)束事務(wù)實例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04golang框架gin的日志處理和zap lumberjack日志使用方式
這篇文章主要介紹了golang框架gin的日志處理和zap lumberjack日志使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01