Go has no exception mechanism like Java, it cannot throw an exception. Because designers think it's easy to confuse the code by mixing anomalies with control structures. The introduction of exception treatment: Defer,panic,recover;
Simple description: Go can throw a panic exception, then catch the exception through recover in defer, and then handle it normally.
Example code:
package mainimport "fmt"func demo(){ fmt.Println("Welcome, my name is fudomine") panic(400) fmt.Println("Hello")}func main(){ defer func(){ fmt.Println("defer start") errorMsg := recover() fmt.Println("Error msg :", errorMsg) fmt.Println("defer end") }() demo()}
Run results
Defer
Defer English original meaning: VI. Defer, postpone, obey Vt. Make a postponement of; postpone.
- A built-in function in the go language, similar to C/php's destructor, but not for objects but functions;
- Allows multiple defer statements to be added to the function. When the function executes to the last, these defer statements are executed in reverse order, and finally the function returns (like a stack, advanced, and out).
- Call must be called before the end of the program, simply to let the defer statement written to memory before Return/panic;
func demo()(str string){ str = "Hello" defer func() { str = "Welcome, my name is fudomine" }() return}
Return result: Welcome, my name is Fudomine
func demo()(str string){ str = "Hello" return defer func() { str = "Welcome, my name is fudomine" }() return}
return Result: Hello
May have a small partner to write their own code, the answer is inconsistent; for this, the special results of a different example code and analysis;
func demo()(string){ str := "Hello" defer func() { str = "Welcome, my name is fudomine" }() return str}
Cause: Defer return XXX This statement is not an atomic directive, and the result above is because the declaration of the output type is *t (pointer) at initialization time;
Split into:
- return value = XXX
- Call the Defer function
- An empty return
For details, please refer to the "Go in depth analysis" article
Panic
Panic English original meaning: N. Panic, adj, panic, fear, no reason, Vt. Make Panic VI. Very alarmed.
- Go is a built-in function that interrupts the original control flow and enters an abnormal process. When the function Func calls panic, the execution of the function func is interrupted, but the delay function in Func executes normally, and then Func returns to where it was called. Where called, the Func behaves as if it were called panic. This process continues up (only just defer inches in memory and will be called) until all the called functions in the goroutine that occur panic return, and the program exits.
Recover
Recover English Original: Vt. To recover from, to make up for or regain VI. To regain the ball N. Revert to a ready posture
- Go is a built-in function that allows Goroutine to recover from a panic-entering process. The recover is only valid in the delay function. During normal execution, the call to recover returns nil and has no other effect. If the current goroutine is in panic, call recover can capture the input value of panic and return to normal execution.
Reference documents
"Go Web Programming"
Author: not moving Peak
Blog Park: http://www.cnblogs.com/mylly/
All rights reserved, welcome to keep the original link to reprint:)