溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Golang中panic與recover的區(qū)別是什么

發(fā)布時(shí)間:2022-06-08 14:15:12 來源:億速云 閱讀:131 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“Golang中panic與recover的區(qū)別是什么”,在日常操作中,相信很多人在Golang中panic與recover的區(qū)別是什么問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Golang中panic與recover的區(qū)別是什么”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

前言

與defer類似的是,goroutine 中也有一個(gè)_panic鏈表頭指針指向一個(gè)_panic鏈,發(fā)生panic的時(shí)候也是在鏈表頭插入_panic結(jié)構(gòu)體(執(zhí)行g(shù)opanic)

在執(zhí)行過程中發(fā)生了panic。那么panic以后的代碼不會執(zhí)行,轉(zhuǎn)而執(zhí)行panic的邏輯,再執(zhí)行defer,執(zhí)行到的defer要將started標(biāo)記為true,同時(shí)將其defer結(jié)構(gòu)體中的_panic指針指向當(dāng)前的_panic,表示這個(gè)defer是由該panic觸發(fā)的。再去執(zhí)行defer鏈表,如果defer執(zhí)行中還觸發(fā)了panic,panic后的代碼不載執(zhí)行,將這個(gè)panic插入panic鏈頭,同時(shí)將其作為當(dāng)前panic。當(dāng)遇到了與當(dāng)前panic不符的defer,就找到該defer上的panic,將其標(biāo)記為已終止,從defer鏈表中移除當(dāng)前執(zhí)行的defer。打印panic移除信息,從鏈表尾開始逐步輸出

流程

panic執(zhí)行defer的流程:

  • 先標(biāo)記started=true,_panic=&panic

  • 后釋放

  • 目的是為了終止之前發(fā)生的panic

異常信息的輸出方式:

  • 所有還在panic鏈表上的項(xiàng)會被輸出

  • 順序與發(fā)生panic的順序一致

Golang中panic與recover的區(qū)別是什么

// 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 存儲當(dāng)前要執(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 標(biāo)識panic是否被恢復(fù)
    recovered bool           // whether this panic is over
    // aborted 標(biāo)識panic是否被終止
	aborted   bool           // the panic was aborted
	goexit    bool
}

關(guān)于recover

recover只執(zhí)行一件事

  • 將當(dāng)前執(zhí)行的panic的recovered字段置為true

在每個(gè)defer執(zhí)行完以后panic處理流程都會檢查當(dāng)前panic是否被recover

  • 如果當(dāng)前panic已經(jīng)被恢復(fù),就會將它從panic鏈中移除

  • 執(zhí)行到的defer也會被移除,同時(shí)要保存_defer.sp和_defer.pc

利用_defer.sp和_defer.pc跳出當(dāng)前panic的處理流程,通過棧指針判斷,只執(zhí)行當(dāng)前函數(shù)中注冊的defer函數(shù)

Golang中panic與recover的區(qū)別是什么

在發(fā)生recover的函數(shù)正常結(jié)束后才會進(jìn)入到檢測panic是否被恢復(fù)的流程

當(dāng)recover的函數(shù)又發(fā)生panic時(shí),goroutine會將該panic加入到鏈頭,設(shè)置為當(dāng)前panic,再去執(zhí)行defer鏈表,發(fā)現(xiàn)當(dāng)前defer是當(dāng)前panic執(zhí)行的,移除當(dāng)前defer,繼續(xù)執(zhí)行下一個(gè),直到發(fā)現(xiàn)不是當(dāng)前panic執(zhí)行的,在panic鏈上找到那個(gè)panic,輸出異常信息

對于已經(jīng)recover標(biāo)記的panic在輸出異常信息時(shí)會加上recovered標(biāo)記

Golang中panic與recover的區(qū)別是什么

到此,關(guān)于“Golang中panic與recover的區(qū)別是什么”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI