Re: [go-nuts] Re: Unexpected behavior with panic/defer/recover
There is also one additional caveat (not for your example, but just in case you stumble upon it): recover must be called *from a deferred function, *meaning "defer recover()" won't work either. You *always* need to wrap recover in a function and that function *always* needs to be directly defer'd. On Wed, Mar 29, 2017 at 4:21 AM, T L wrote: > http://www.tapirgames.com/blog/golang-panic-recover-mechanism > > > On Wednesday, March 29, 2017 at 6:48:52 AM UTC+8, Stefan Engstrom wrote: >> >> Playing with recovery from a panic - this code does what i expect: ( >> https://play.golang.org/p/p5KvOYc1sx) >> >> package main >> >> 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) >> >> package main >> >> import "fmt" >> >> func main() { >> defer fmt.Println("main:",recover()) >> a() >> } >> >> func a() { >> panic("yikes") >> } >> >> Produces: >> >> main: >> panic: yikes >> >> goroutine 1 [running]: >> main.a() >> /tmp/sandbox638763687/main.go:11 +0x60 >> main.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. > 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.
[go-nuts] Re: Unexpected behavior with panic/defer/recover
http://www.tapirgames.com/blog/golang-panic-recover-mechanism On Wednesday, March 29, 2017 at 6:48:52 AM UTC+8, Stefan Engstrom wrote: > > Playing with recovery from a panic - this code does what i expect: ( > https://play.golang.org/p/p5KvOYc1sx) > > package main > > 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) > > package main > > import "fmt" > > func main() { > defer fmt.Println("main:",recover()) > a() > } > > func a() { > panic("yikes") > } > > Produces: > > main: > panic: yikes > > goroutine 1 [running]: > main.a() > /tmp/sandbox638763687/main.go:11 +0x60 > main.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. For more options, visit https://groups.google.com/d/optout.
[go-nuts] Re: Unexpected behavior with panic/defer/recover
That's kind of expected behavior , "*1. A deferred function's arguments are evaluated when the defer statement is evaluated." * refer to https://blog.golang.org/defer-panic-and-recover On Wednesday, March 29, 2017 at 9:48:52 AM UTC+11, Stefan Engstrom wrote: > > Playing with recovery from a panic - this code does what i expect: ( > https://play.golang.org/p/p5KvOYc1sx) > > package main > > 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) > > package main > > import "fmt" > > func main() { > defer fmt.Println("main:",recover()) > a() > } > > func a() { > panic("yikes") > } > > Produces: > > main: > panic: yikes > > goroutine 1 [running]: > main.a() > /tmp/sandbox638763687/main.go:11 +0x60 > main.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. For more options, visit https://groups.google.com/d/optout.