前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Go 异常捕获处理: panic(err) 与 recover()

Go 异常捕获处理: panic(err) 与 recover()

作者头像
一个会写诗的程序员
发布2022-05-31 10:24:26
6810
发布2022-05-31 10:24:26
举报
文章被收录于专栏:一个会写诗的程序员的博客

代码实例

代码语言:javascript
复制
func (rcvr *CQL) Compile(ctx context.Context) string {
    defer func() {
        if err := recover(); err != nil {
            //打印调用栈信息
            buf := make([]byte, 2048)
            n := runtime.Stack(buf, false)
            stackInfo := fmt.Sprintf("%s", buf[:n])
            logu.CtxError(ctx, error_code.ProcessError, "Compile", "[FillStructInfo] rcvr=%v, resultRecord=%v, panic recovered: \n%v\n%v", convert.ToJSONString(rcvr), err, stackInfo)
        }
    }()

    return rcvr.compile1()
}

panic / recover 源码注释

代码语言:javascript
复制
// The panic built-in function stops normal execution of the current
// goroutine. When a function F calls panic, normal execution of F stops
// immediately. Any functions whose execution was deferred by F are run in
// the usual way, and then F returns to its caller. To the caller G, the
// invocation of F then behaves like a call to panic, terminating G's
// execution and running any deferred functions. This continues until all
// functions in the executing goroutine have stopped, in reverse order. At
// that point, the program is terminated with a non-zero exit code. This
// termination sequence is called panicking and can be controlled by the
// built-in function recover.
func panic(v any)

// The recover built-in function allows a program to manage behavior of a
// panicking goroutine. Executing a call to recover inside a deferred
// function (but not any function called by it) stops the panicking sequence
// by restoring normal execution and retrieves the error value passed to the
// call of panic. If recover is called outside the deferred function it will
// not stop a panicking sequence. In this case, or when the goroutine is not
// panicking, or if the argument supplied to panic was nil, recover returns
// nil. Thus the return value from recover reports whether the goroutine is
// panicking.
func recover() any

runtime.Stack(buf []byte, all bool) int

代码语言:javascript
复制
// Stack formats a stack trace of the calling goroutine into buf
// and returns the number of bytes written to buf.
// If all is true, Stack formats stack traces of all other goroutines
// into buf after the trace for the current goroutine.
func Stack(buf []byte, all bool) int {
    if all {
        stopTheWorld("stack trace")
    }

    n := 0
    if len(buf) > 0 {
        gp := getg()
        sp := getcallersp()
        pc := getcallerpc()
        systemstack(func() {
            g0 := getg()
            // Force traceback=1 to override GOTRACEBACK setting,
            // so that Stack's results are consistent.
            // GOTRACEBACK is only about crash dumps.
            g0.m.traceback = 1
            g0.writebuf = buf[0:0:len(buf)]
            goroutineheader(gp)
            traceback(pc, sp, 0, gp)
            if all {
                tracebackothers(gp)
            }
            g0.m.traceback = 0
            n = len(g0.writebuf)
            g0.writebuf = nil
        })
    }

    if all {
        startTheWorld()
    }
    return n
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-05-31,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 代码实例
  • panic / recover 源码注释
  • runtime.Stack(buf []byte, all bool) int
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档