This example describes the go language exception panic and recovery recover usage. Share to everyone for your reference. The specific analysis is as follows:
Go can throw a panic exception, then catch the exception in defer by recover, and then handle the normal
In the structure of a main process, multiple go process logic, this is important if the panic exception is not captured by recover, causing the entire process to be faulted
Copy Code code as follows:
Package Main
Import "FMT"
Func Main () {
Defer func () {///You must declare the defer first, otherwise you cannot catch the panic exception
Fmt. Println ("C")
If err: = Recover (); Err!= Nil {
Fmt. PRINTLN (ERR)//Here err is actually panic incoming content, 55
}
Fmt. Println ("D")
}()
F ()
}
Func f () {
Fmt. Println ("a")
Panic (55)
Fmt. Println ("B")
Fmt. Println ("F")
}
I hope this article will help you with your go language program.