Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Go language-practical use of panic and recover

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report