In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge about "what is the method of exception handling in Go language". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to handle these situations! I hope you can read carefully and learn something!
Go seeks simplicity and elegance, so it doesn't support traditional try…catch…finally exceptions because Go's designers believe that mixing exceptions with control structures makes it easy to clutter code. Because developers can easily abuse exceptions, even a small error throws an exception.
In Go, using multivalued fallback returns errors. Don't replace errors with exceptions, and don't use them to control flow. In rare cases, that is, when a true exception is encountered (e.g., when the divisor is 0). Exception handling introduced in Go: defer, panic, recover.
The usage scenarios of these exceptions can be described as follows: Go can throw a panic exception, and then catch this exception in defer by recovering, and then handle it normally.
Example code:
package mainimport "fmt"func main(){ defer func(){ //defer must be declared first, otherwise panic exception cannot be caught fmt.Println("c") if err:=recover();err!= nil{ fmt.Println(err) //where err is actually what panic passes in, 55 } fmt.Println("d") }() f()}func f(){ fmt.Println("a") panic(55) fmt.Println("b") fmt.Println("f")} Output result: acdexit code 0, process exited normally.
defer
Defer English original meaning: vi. defer v. defer v. To postpone; postpone.
The idea behind defer is similar to that of destructors in C++, except that Go "destructs" functions rather than objects, and defer is used to add statements that execute at the end of a function. Note that the emphasis here is on adding, not specifying, because unlike destructors in C++, which are static, defer in Go is dynamic.
func f() (result int) { defer func() { result++ }() return 0}
The above function returns 0 because it returns before it can add anything defer.
It is also worth mentioning that defer can be repeated many times, thus forming a defer stack, and the statements after defer will be called first when the function returns.
panic
Panic is used to indicate a very serious, irrecoverable error. In Go this is a built-in function that takes a value of type interface{}(i.e. any value) as an argument. Panic acts like an anomaly we normally encounter.
Go doesn't have a try…catch, though, so panics usually cause programs to hang (unless recovered). So, exceptions in Go, that's really an exception. You can try it, call panic and see, the program immediately hangs, and then Go will print out the call stack when it runs.
However, the key point is that even if the function panic when executing, the function does not go down, the runtime does not immediately pass panic up, but to defer that, and so on defer things run out, panic and then pass up. Defer is like finally in try-catch-finally.
recover
As mentioned above, the panic function does not return immediately, but defers first and then returns. At this point (when defer), if there is a way to catch the panic and prevent the panic from passing, then the exception handling mechanism is perfect.
Go provides the recover built-in function, mentioned earlier, once panic, logic will go to defer, then we wait at defer, call recover function will catch the current panic (if any), caught panic will not be passed up, so the world is restored to peace. You can do whatever you want.
However, it should be noted that after recover, the logic does not return to the panic point, and the function will still return after defer.
"What is the method of exception handling in Go language" is introduced here. Thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
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.