func TestRecover(t *testing.T) {
f := func() (interface{}, error) {
panic(`TEST`)
}
r, e := Recover(f)
t.Log(r, e)
}
func Recover(f func() (interface{}, error)) (interface{}, error) {
var result interface{}
var err error
defer func() {
if e := recover(); e != nil {
err = errors.Error(fmt.Sprintf("%v", e))
}
}()
result, err = f()
return result, err
}
func Recover(f func() (interface{}, error)) (res interface{}, err error) {
defer func() {
if e := recover(); e != nil {
err = errors.Error(fmt.Sprintf("%v", e))
}
}()
res, err = f()
return res, err
}
If the deferred function has any return values, they are discarded when the function completes. (See also the section on handling panics.)
https://golang.org/ref/spec#Function_declarationsIf the function's signature declares result parameters, the function body's statement list must end in a terminating statement.
For instance, if the deferred function is a function literal and the surrounding function has named result parameters that are in scope within the literal, the deferred function may access and modify the result parameters before they are returned.
The recover function allows a program to manage behavior of a panicking goroutine. 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. If D returns normally, without starting a new panic, the panicking sequence stops. In that case, the state of functions called between G and the call to panic is discarded, and normal execution resumes. Any functions deferred by G before D are then run and G's execution terminates by returning to its caller.
--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.