In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-15 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.