Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Go language-practical use of panic and recover

Shulou Source: shulou.com Published: 2022-06-03 05:00:22 09月11日 Update

Panic

The normal function execution process will be terminated immediately, but the statement in the function that was delayed with the defer keyword will unfold normally, and then the function will return to the calling function and cause the panic () process to be executed layer by layer until all executing functions in the goroutine to which it belongs will be terminated. Error messages will be reported, including the parameter type interface () of panic (). We can see that panic can receive any type of data.

Panic (404)

Panic ("network borken")

Panic ("Error (" file not exists ")

Recover

The recover function is used to terminate the error handling process. Recover should be executed in the function of the defer keyword to effectively intercept the error handling flow

Instance 1package mainimport ("fmt") func main () {defer func () {if r: = recover (); r! = nil {fmt.Println ("detail:", r)} () fmt.Println ("before painc") panic ("error1") panic ("error2") fmt.Println ("after panic") return}

Running result

Before paincdetail: error1 instance 2package mainimport ("fmt") func main () {fmt.Println ("before painc") panic ("error1") panic ("error2") fmt.Println ("after panic") return}

Running result

Before paincpanic: error1goroutine 1 [running]: main.main () / home/kenmy/go/src/github.com/shadowsocks/shadowsocks-go/sample-config/panic.go:17 + 0xdd instance 3package mainimport ("fmt") func main () {defer func () {if r: = recover () R! = nil {fmt.Println ("detail:", r)} () test () fmt.Println ("end") return} func test () {defer func () {if r: = recover () R! = nil {fmt.Println ("detail0:", r)} () fmt.Println ("before painc") panic ("error1") panic ("error2") fmt.Println ("after panic")}

Running result

Before paincdetail0: error1end instance 4package mainimport ("fmt") func main () {defer func () {if r: = recover () R! = nil {fmt.Println ("detail:", r)} () test () fmt.Println ("middle") panic ("error3") fmt.Println ("end") return} func test () {defer func () {if r: = recover () R! = nil {fmt.Println ("detail0:", r)} () fmt.Println ("before painc") panic ("error1") panic ("error2") fmt.Println ("after panic")}

Running result

Before paincdetail0: error1middledetail: error3 instance 5package mainimport ("fmt") func main () {test () fmt.Println ("middle") panic ("error3") fmt.Println ("end") return} func test () {fmt.Println ("before painc") panic ("error1") panic ("error2") fmt.Println ("after panic")}

Running result:

Before paincpanic: error1goroutine 1 [running]: main.test () / home/kenmy/go/src/github.com/shadowsocks/shadowsocks-go/sample-config/panic.go:20 + 0xddmain.main () / home/kenmy/go/src/github.com/shadowsocks/shadowsocks-go/sample-config/panic.go:10 + 0x26

Tags: Function instance result run process error key keyword type processing valid information parameter ownership report data being statement delay actual combat Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Shulou Tech Info Redmi macOS Shulou Information