企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] ## demo 在模块内部直接使用 panic ,外部通过 recover 接受异常并返回错误 ``` func Unmarshal() (err error) { defer func() { if r := recover(); r != nil { err = r.(error) } }() a() return nil } func a() { panic(errors.New("this is a error")) } func main() { e := Unmarshal() if e != nil { fmt.Printf("%+v\n", e) } time.Sleep(1 * time.Second) fmt.Println("end") //output //this is a error //end } ```