Hello. You can achieve this by using a named return value and overwriting it in a deferred function, like so: https://play.golang.org/p/4HvVQ6N-I9
Note that the value returned by recover() is not guaranteed to be an error. In my toy example, I work around this fact by passing it to fmt.Errorf in order to produce an error. This feels somewhat brittle and unpleasant. On Friday, March 24, 2017 at 6:13:57 AM UTC+1, Henry wrote: > > Hi, > > I would like to shield my application from any possible panicking by the > third-party library. There was an occasion when a third party library > crashed my application due to unnecessary panicking. I am thinking > of creating a thin wrapper that will also catch any panic into error. How > can I do that in Go? > > func PanicFunction() { > panic("Panic!") > } > > func MustNotPanic() error { > defer func(){ > if err:=recover(); err!=nil{ > //ermm ... how do you return error from here? > } > }() > PanicFunction() > } > > func main(){ > if err:=MustNotPanic();err==nil{ > fmt.Println("Failed to catch panic") > return > } > fmt.Println("success") > } > > Thanks. > -- 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.