-
Notifications
You must be signed in to change notification settings - Fork 18.9k
Description
What version of Go are you using (go version)?
Go playground
What did you do?
package main
import (
"fmt"
)
func main() {
defer func() {
defer func() {
fmt.Println(recover())
}()
}()
panic("abcd")
}See https://play.golang.org/p/0BULKAck6b6.
The recover returns nil, and the program panics.
What did you expect to see?
From the spec:
Suppose a function G defers a function D that calls recover and a panic occurs in a function on the same goroutine in which G is executing. When the running of deferred functions reaches D, the return value of D's call to recover will be the value passed to the call of panic.
In this case, G is the function deferred in main, and D is the func() { fmt.Println(recover()) } which is deferred in G. When there is the panic in the same goroutine the G runs, the recover does not recover the panic.
I did some experiments and it seems the recover in D only recovers panics inside G, assuming D is deferred in G. So probably the spec can be more accurate.