Go Language Panic&recover

Source: Internet
Author: User
panic//file directory: $GOPATH/src/panic/panic.go/*1 array Access out-of-bounds, null pointer caused PANIC exception 2 PANIC exception, program interrupted run, and immediately execute goroutine defer function, Then the program crashes output log information: Panic value, function call stack information; Panic value is usually an error message, the log information provides sufficient diagnostic tools, panic exception and log information are recorded to the report; 3 input value is NULL interface: Func pan IC (v interface{}) 4 Recover output value is NULL interface: Func recover () interface{}5 panic and recover English explanation 5.1 when panic was called, it Immedi ately stops execution of the//panic exception, program rollback Goroutine stack, execute stack defer function current function and begins unwinding the stack o    f the Goroutine, running any deferred functions along the.    If you roll back to the top of the stack, the program (goroutine) dies if that unwinding reaches the top of the Goroutine ' s stack, the programs dies. The Recover function can re-control goroutine and return to normal execution order 5.2 However, it is possible to use the built-in function recover to regain cont    Rol of the goroutine and resume normal execution. Recover can only be placed in the defer function, because panic will rollback defer function A call to recover stops the unwinding and returns the argument passed to pan IC. Because the only code, runs while UNWInding is inside deferred functions, recover are only useful inside deferred functions. 5.3 Recover can be used to turn off failed goroutine without affecting other Goroutine one application of recover is to shut down a failing goroutine inside a    Server without killing the other executing goroutines.*/package mainimport ("FMT" "OS" "Runtime") func main () { After the defer function panic does not output stack information defer func () {p: = recover () if p! = Nil {fmt. Println ("Panic Value:", P)}} ()//Add Print stack information function to output stack information defer printstack () F (3)}func f (x int) {FMT. Printf ("f (%d) \ n", x+0/x)//panics if x = = 0 Defer fmt. Printf ("Defer%d\n", X) F (x-1)}func printstack () {var buf [4096]byte N: = runtime. Stack (buf[:], False) OS. Stdout.write (buf[:n])}//recover//file directory: $GOPATH/src/recover/panicrecv.go/*1 defer function call RECOVER, defer program exits normally or panic execution: Defer func () {if p:= recover (); P! = nil{};} () 1.1 Recover causes the program to recover from panic and returns panic value 1.2 that causes the panic exception function to not continue to run, but normally returns 1.3 without panic when called Recover,reCover returns NIL2 to panic value as a special token, when the recover received P-value for the tag value is processed, and other conditions continue to be abnormal: defer func () {switch P: = Recover (); p{ Non-panic exception case NIL://panic value is the tag values, execution recover recovery + processing statement case bailout{}: Err: = FMT. Errorf ("The error is ...")//panic exception and not tag value continue panic Default:panic (P)}} () */package            Mainimport "FMT" func t1 (i int) (j int) {defer func () {if p: = Recover (); p! = Nil {i = 1-i j = 1}} () If I <= 0 {panic ("please input a int >0")} j = I return J//retu RN UINT (i)}func main () {FMT. PRINTLN (T1 (-1))}

78 reads ∙1 likes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.