With the "Subversion storm" caused by the blockchain, a large number of blockchain training institutions emerged. But in a mixed-up training circle, it is bucket to find a truly standard course system and a dedicated professional lecturer in the Blockchain field. Brother even education pointed out that it is time to make action to change and subvert the traditional training institutions operating thinking, and remind the public users, should be rational choice of blockchain training institutions.
Func Panic (interface{}) and Func recover () interface{} are the two functions used for error handling in Golang.
The function of panic is to throw an error message, and from its argument type it can be seen to throw any type of error message. When panic is called somewhere in the execution of the function, an error message is immediately thrown, and the normal execution of the function terminates, but the defer statement panic previously defined in the function is executed sequentially. The goroutine immediately stops executing.
Recover () is used to capture information from panic. Recover must be defined in the defer statement before panic. In this case, when panic is triggered, the goroutine will not simply terminate, but will execute the defer statement defined before it.
The following is a simple example:
--Capture your own set of panic errors:
Package Main
Import "FMT"
Import "Math"
Func foo (a int) {
Defer FMT. Println ("foo exits")
Defer func () {
If r: = Recover (); R! = Nil {
Fmt. Printf ("Caught Error:%s\n", R)
}
}()
If a < 0 {
Panic ("must enter a number greater than 0")
}
Fmt. Println ("The root of the number is:", Math.) Sqrt (Float64 (a)))
}
Func Main () {
var a int
A = 10
Fmt. Printf ("a=%d\n", a)
Foo (a)
var b int
b =-10
Fmt. Printf ("b=%d\n", b)
Foo (b)
Fmt. Println ("The goroutine can also be executed")
}
// ///////////
a=10
The square root of the number is: 3.1622776601683795
Foo's out of here.
B=-10
Caught error: Must enter a number greater than 0
Foo's out of here.
The Goroutine can also perform
Process finished with exit code 0
----
--Captures panic errors inside the Go language:
Package Main
Import "FMT"
Func foo () {
Defer func () {
If r: = Recover (); R! = Nil {
Fmt. Printf ("Caught Error:%s\n", R)
}
}()
var a, b int
A, B = 1, 1
c: = 3/(A-B)
Fmt. Println (A, B, c)
}
Func Main () {
Foo ()
}
//====
Caught error: Runtime Error:integer divide by zero
"