go - Is it possible to recover from a panic inside a panic? -
it looks it's not possible recover panic inside panic?
func testerror(t *testing.t) { e := &myerr{p: false} fmt.println(e.error()) // prints "returned" panic(e) // prints "panic: returned" e1 := &myerr{p: true} fmt.println(e1.error()) // prints "recovered" panic(e1) // prints "panic: panic: paniced // fatal error: panic holding locks // panic during panic" } type myerr struct { p bool } func (m *myerr) error() (out string) { defer func() { if r := recover(); r != nil { out = "recovered" } }() if m.p { panic("paniced") } return "returned" }
backstory: error error() function uses os.getwd, seems panic when inside panic, i'd handle gracefully.
i think solves problem replace this
panic(e1)
by this
panic(e1.error())
playground: http://play.golang.org/p/fxpx2ch9ef
and question here interesting: why? hard part , don't know exact answer. problem here panicing after deffered functions executed (so when go try print panic error string). correction welcomed if it's not correct!
Comments
Post a Comment