普通网友 2014-06-05 22:03
浏览 31
已采纳

将恐慌/恢复为投掷/接球是错误的

Speaking as a new go enthusiast trying to work with the go way of error handling. To be clear - I like exceptions.

I have a server that accepts a connection , processes a set of requests and replies to them. I found that I can do

if err != nil{
      panic(err)
}

in the deep down processing code

and have

defer func() {
        if err := recover(); err != nil {
            log.Printf("%s: %s", err, debug.Stack()) // line 20
        }
    }()

in the client connection code (each connection is in a goroutine). This nicely wraps everything up, forcefully closes the connection (other defers fire) and my server continues to hum along.

But this feels an awful lot like a throw/catch scenario - which golang states it doesn't support. Questions

  • is this stable. ie recovering a panic is an OK thing to do as an ongoing way of life. Its not intended to just slightly defer an immediate shutdown
  • I looked for a discussion on this topic and did not find it anywhere - any pointers?

I feel that the answer is 'yes it works' and can be used inside you own code, but panic should NOT be used by a library intended for wider use. The standard and polite way for a library to behave is by error returns

  • 写回答

2条回答 默认 最新

  • doujing2497 2014-06-05 22:38
    关注

    Yes, you can do what you suggest. There are some situations within the standard packages where panic/recover is used for handling errors. The official Go blog states:

    For a real-world example of panic and recover, see the json package from the Go standard library. It decodes JSON-encoded data with a set of recursive functions. When malformed JSON is encountered, the parser calls panic to unwind the stack to the top-level function call, which recovers from the panic and returns an appropriate error value (see the 'error' and 'unmarshal' methods of the decodeState type in decode.go).

    Some pointers:

    • Use error for your normal use cases. This should be your default.
    • If your code would get clearer and simpler by using a panic/recover (such as with a recursive call stack), then use it for that particular case.
    • Never let a package leak panics. Panics used within a package should be recovered within the package and returned as an error.
    • Recovering from a panic is stable. Don't worry about continuing execution after a recover. You can see such behavior in standard library such as with the net/http package which recovers from panics within handlers to prevent the entire http server to go crash when panicing on a single request.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题