55

如果函数发生恐慌(在 Go 中),我想从函数返回错误:

func getReport(filename string) (rep report, err error) {
    rep.data = make(map[string]float64)

    defer func() {
        if r := recover(); r != nil {
            fmt.Println("Recovered in f", r)
            err, _ = r.(error)
            return nil, err
        }
    }()
    panic("Report format not recognized.")
    // rest of the getReport function, which can try to out-of-bound-access a slice
    ...
} 

我似乎误解了恐慌和推迟的概念。有人可以启发我吗?

4

3 回答 3

77

在延迟函数中,您可以更改返回的参数,但不能返回新的集合。因此,对您所拥有的内容进行简单的更改即可使其发挥作用。

你写的还有另一个问题,即你对 a 感到恐慌,但在你的类型断言string中期待 a 。error

这是对这两者的修复(Play

defer func() {
    if r := recover(); r != nil {
        fmt.Println("Recovered in f", r)
        // find out exactly what the error was and set err
        switch x := r.(type) {
        case string:
            err = errors.New(x)
        case error:
            err = x
        default:
            err = errors.New("Unknown panic")
        }
        // invalidate rep
        rep = nil
        // return the modified err and rep
    }
}()
于 2013-11-12T16:46:38.587 回答
6

看看这个

package main

import "fmt"

func iWillPanic() {
    panic("ops, panic")
}
func runner() (rtnValue string) {
    rtnValue := ""
    defer func() {
        if r := recover(); r != nil {
            // and your logs or something here, log nothing with panic is not a good idea
            rtnValue = "don't panic" // modify the return value, and it will return
        }
    }()
    iWillPanic()
    return rtnValue
}

func main() {
    fmt.Println("Return Value:", runner())
}
于 2018-06-28T10:45:45.883 回答
3
func TestReturnFromPanic(t *testing.T) {
   fn := func(filename string) (rep string, err error) {
       defer func() {
           if r := recover(); r != nil {
               err = fmt.Errorf("panic in getReport %s", r)
           }
       }()
       return filename[100:], nil
   }
   t.Log(fn(``))
}

命名返回参数err是诀窍。

https://play.golang.org/p/jpaCa9j2iAf

于 2020-03-10T09:16:35.047 回答