You second example isn't calling recover inside the defer.
See: https://golang.org/pkg/builtin/#recover

On 28/03/2017 23:00, Stefan Engstrom wrote:
Playing with recovery from a panic - this code does what i expect: (https://play.golang.org/p/p5KvOYc1sx)

|
packagemain

import"fmt"

func main(){
  defer func(){
    fmt.Println("main:",recover())
}()
  a()
}

func a(){
  panic("yikes")
}
|

Results in:
|
main:yikes
|



A simpler version without the closure doesn't catch the panic: (https://play.golang.org/p/0ICqqdi2kL)

|
packagemain

import"fmt"

func main(){
  defer fmt.Println("main:",recover())
  a()
}

func a(){
  panic("yikes")
}
|

Produces:
|
main:<nil>panic:yikes goroutine 1[running]:main.a()/tmp/sandbox638763687/main.go:11+0x60main.main()/tmp/sandbox638763687/main.go:7+0x100
|

I see the same behavior on linux/amd64 go1.8

Any idea why the second version fails to catch that panic?

Thanks,

Stefan

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com <mailto:golang-nuts+unsubscr...@googlegroups.com>.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to