doubengshao8872 2015-02-11 09:59
浏览 33
已采纳

如何应对外界的恐慌

Look at the following code snippet.

package main

import "fmt"

func explode() {
    // Cause a panic.
    panic("WRONG")
}

func recovery() int {
    explode()
    defer func() {
        fmt.Println("Try to handle panic")
        if err := recover(); err != nil {
            fmt.Println("FIX")
            fmt.Println("ERR", err)
        }
    }()
    fmt.Println("Print value")
    return 100

}

func main() {
    // Handle errors in defer func with recover.
    fmt.Println(recovery())
}

As you can see code above, I fired a panic in the explode function and want to handle it in the recovery function. But the panic does not get caught, I've got runtime error

goroutine 1 [running]:
main.explode()
        D:/gocode/src/samples/panic1.go:7 +0x6b
main.recovery(0xc082002250)
        D:/gocode/src/samples/panic1.go:18 +0x26
main.main()
        D:/gocode/src/samples/panic1.go:27 +0x26

goroutine 2 [runnable]:
runtime.forcegchelper()
        c:/go/src/runtime/proc.go:90
runtime.goexit()
        c:/go/src/runtime/asm_amd64.s:2232 +0x1

goroutine 3 [runnable]:
runtime.bgsweep()
        c:/go/src/runtime/mgc0.go:82
runtime.goexit()
        c:/go/src/runtime/asm_amd64.s:2232 +0x1

goroutine 4 [runnable]:
runtime.runfinq()
        c:/go/src/runtime/malloc.go:712
runtime.goexit()
        c:/go/src/runtime/asm_amd64.s:2232 +0x1
exit status 2

How to catch the panic in the recory function?

  • 写回答

1条回答 默认 最新

  • douningqiu4991 2015-02-11 10:02
    关注

    If you call explode() first, the function that would handle the panic and try to recover is not yet registered (and never will be because you call panic inside explode()), so it won't be called and obviously it can't do its work.

    You have to call defer first and then the explode() function:

    defer func() {
        // recover() here, Your code omitted
    }()
    explode()
    

    Try it on the Go Playground.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大