【发布时间】:2016-01-15 00:25:46
【问题描述】:
我的 Go 函数应该返回一个值,但是在调用库函数时它可能会出现恐慌。我可以使用recover() 在延迟调用中捕获它,但在这种情况下如何返回值?
func MyFunc() string{
defer func() {
if err := recover(); err != nil {
// What do I do to make MyFunc() return a value in case of panic?
}
}()
SomeFuncThatMayPanic()
return "Normal Return Value"
// How can I return "ERROR" in case of panic?
}
【问题讨论】:
标签: go exception-handling recover