In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
What is the role of panic and recover in the Go language, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.
1. Introduction to panic
Once panic is triggered, it is processed in the following order:
1) start the termination program operation where the panic starts.
2) call the defer function in the current trigger panic function.
3) return the caller of the function as an exception, so this step also calls the defer of the caller function until there is no caller.
4) print the information of panic.
5) print stack trace information, that is, the function call relationship we see.
6) terminate the program.
Example:
Result analysis: through the output result, we can see that 1) 2 called panic to start the panic operation, 3 out is not executed, so there is no printing. 2) execute the defer function in testPanic and output "testPanic exit!". 3) then, return to the caller's main function as an abnormal termination, call the defer function in main, print "main exit!", and the six codes in main are also skipped. 4) print the error message "trigger panic!" when panic is triggered. 5) print the call stack information for panic, which starts from the point where the panic is triggered and is called up layer by layer. 6) exit the program.
2. Introduction to recover
Recover is a function provided by go to intercept panic information and retrieve co-program control. There are two things to note about its use: 1) recover can only be used in defer functions. 2) the use of recover must be the same as the one that triggers the panic.
Example 1, in the same collaborative process:
Result analysis: after calling recover, panic was successfully intercepted by recover at 1, but the code was not executed at 3 points. For the subsequent code of main function that called testPanic, all the subsequent code was executed, "main end!" The information has also been printed out.
Example 2 in different collaborative processes, recover panic and recover cannot recover.
Result analysis: from the output result, we can see that even if we call rcover in 4 places, panic is not intercepted. The reason is that panic is in the subprogram, while recover in the main program, recover cannot intercept panic information across the synergy.
3. Additional knowledge:
Debug.PrintStack () can also do the function stack call relationship shown by panic, which is used to print the stack call relationship of the function, as shown in the following example:
The following is an explanation of the panic and recover functions: https://golang.org/src/builtin/builtin.go?h=panic#L232
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.