2016-08-05 22:00
浏览 308


Is there any way to catch stack overflow error in golang? Currently I'm using go recover() do this job(below code snippet), looks like stack overflow error can't be caught.

defer func() {
            if x := recover(); x != nil {
                log.Error("In recover, cought error====================", x)


图片转代码服务由CSDN问答提供 功能建议

有什么方法可以捕获golang中的堆栈溢出错误? 目前,我正在使用go recovery()来执行此工作(在代码段下面),看来无法捕获堆栈溢出错误。

  defer func(){
 如果x:= recovery();  x!= nil {
}()\  n 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dshmvqnl98119 2016-08-05 22:16

    What you see in the output is a "fatal error", not a panic.

    fatal error: stack overflow

    You can only use recover() to recover from panics. A stack overflow is a fatal error thrown by the runtime which causes the process to print a stack trace and exit.

    打赏 评论
  • dongzi1209 2016-08-07 18:07

    In Golang there are two ways of error propagation one is to return error which is mostly function dependent non fatal errors. Other one is to panic which is propagated through function call stack till end of the process or till it reached recover

    Panic is a built-in function that stops the ordinary flow of control and begins panicking. When the function F calls panic, execution of F stops, any deferred functions in F are executed normally, and then F returns to its caller. To the caller, F then behaves like a call to panic. The process continues up the stack until all functions in the current goroutine have returned, at which point the program crashes. Panics can be initiated by invoking panic directly. They can also be caused by runtime errors, such as out-of-bounds array accesses.

    Recover is a built-in function that regains control of a panicking goroutine. Recover is only useful inside deferred functions. During normal execution, a call to recover will return nil and have no other effect. If the current goroutine is panicking, a call to recover will capture the value given to panic and resume normal execution.

    If you want to ignore a panic you may call recover and inspect the panic message to see if the panic message is of an expected type which is ok to ignore

    Here is how you may do it

    //RecoverPanic recovers from panic given the message String
    //if no message string is given it recovers from all panics
    //  defer RecoverPanic("stack overflow")
    //The above satatement recovers any function from panic that happen
    //due to stackoverflow
    func RecoverPanic(msg string) {
            if r := recover(); r != nil {
                    if msg != "" {
                            if r != msg {

    After this you may call,at the beginning of function call

    funtion foo(){
        defer RecoverPanic("stack overflow")

    There are sometimes panic could not be hadled,for example panic on trying to allocate memory due to insufficient memory that should not be tried to handle as the function to handle itself may panic as there is no memory to hold its data

    打赏 评论
  • dpyic24480 2019-07-14 22:02

    I think JimB is the best answer here. If you have an external library throwing this error, you need to:

    A - Send a PR against that library to fix it. B - Create your own version of the library with no errors.

    I strongly suggest that, before you integrate any external libraries in your code, you test it with Unit tests and Benchmarks to check if it is a deployable lib.

    BTW: I'm creating a sort of videos about Golang development and I'd really like to have your opinion on the topics I should cover. If you like, you can complete this form to let me know what you want to have covered!


    打赏 评论

相关推荐 更多相似问题