Which one is better for me to use: "defer-panic-recover" or checking "if err != nil { //dosomething}" in golang?

huangapple go评论94阅读模式
英文:

Which one is better for me to use: "defer-panic-recover" or checking "if err != nil { //dosomething}" in golang?

问题

我做了一个大型程序,打开和关闭文件和数据库,对它们进行写入和读取等操作。由于Go语言中没有"异常处理"这样的机制,而且我之前并不了解"defer"语句和"recover()"函数,所以我在每次文件打开、读写和数据库操作等之后都进行了错误检查。例如:

_,insert_err := stmt.Run(query)
if insert_err != nil{
    mylogs.Error(insert_err.Error())
    return db_updation_status
}

为此,我在程序开始时将db_updation_status定义为"false",直到程序中的所有操作都正确执行后才将其设置为"true"。
我在每个函数中都这样做,在我认为可能出错的每个操作之后都进行了错误检查。

你认为使用defer-panic-recover有更好的方法吗?我在这里阅读了关于它们的内容http://golang.org/doc/articles/defer_panic_recover.html,但不太清楚如何使用它们。这些结构提供类似于异常处理的功能吗?不使用这些结构会更好吗?
如果有人能用简单的语言解释一下这个问题,并/或者提供一个使用这些结构的用例,并将其与我上面使用的错误处理方式进行比较,我将非常感激。

英文:

I've made a large program that opens and closes files and databases, perform writes and reads on them etc among other things. Since there no such thing as "exception handling in go", and since I didn't really know about "defer" statement and "recover()" function, I applied error checking after every file-open, read-write, database entry etc. E.g.

_,insert_err := stmt.Run(query)
if insert_err != nil{
    mylogs.Error(insert_err.Error())
    return db_updation_status
}

For this, I define db_updation_status at the beginning as "false" and do not make it "true" until everything in the program goes right.
I've done this in every function, after every operation which I believe could go wrong.

Do you think there's a better way to do this using defer-panic-recover? I read about these here http://golang.org/doc/articles/defer_panic_recover.html, but can't clearly get how to use them. Do these constructs offer something similar to exception-handling? Am I better off without these constructs?
I would really appreciate if someone could explain this to me in a simple language, and/or provide a use case for these constructs and compare them to the type of error handling I've used above.

答案1

得分: 6

返回的翻译结果如下:

更方便的是返回error值 - 它们可以携带比两个值的bool更多的信息(对客户/用户有优势)。

关于panic/recover:在某些情况下,它们的使用是完全合理的。例如,在手写的递归下降解析器中,通过所有调用级别“冒泡”出错误条件是相当麻烦的。在这个例子中,如果在最顶层(API级别)有一个延迟的recover,并且可以使用任何调用级别上的任何错误报告,那么这是一个受欢迎的简化,例如:

panic(fmt.Errorf("在 %v 中无法 %v", bar, foo))
英文:

It's more handy to return error values - they can carry more information (advantage to the client/user) than a two valued bool.

What concerns panic/recover: There are scenarios where their use is completely sane. For example, in a hand written recursive descent parser, it's quite a PITA to "bubble" up an error condition through all the invocation levels. In this example, it's a welcome simplification if there's a deferred recover at the top most (API) level and one can report any kind of error at any invocation level using, for example

panic(fmt.Errorf("Cannot %v in %v", foo, bar))

答案2

得分: 2

如果一个操作可能失败并返回错误,那么立即检查这个错误并正确处理它是Go语言中的惯用方式,简单而且方便检查是否正确处理了任何事情。

不要使用defer/recover来处理这种情况:需要清理操作很难编码,特别是当嵌套的东西变多时。

英文:

If an operation can fail and returns an error, than checking this error immediately and handling it properly is idiomatic in go, simple and nice to check if anything gets handled properly.

Don't use defer/recover for such things: Needed cleanup actions are hard to code, especially if stuff gets nested.

答案3

得分: 1

通常报告错误给调用者的方法是作为额外的返回值返回一个错误。典型的Read方法就是一个众所周知的例子;它返回一个字节数和一个错误。
但是如果错误是无法恢复的呢?有时候程序根本无法继续执行。
为此,有一个内置的panic函数,它实际上会创建一个运行时错误,使程序停止(但请参阅下一节)。该函数接受一个任意类型的单个参数 - 通常是一个字符串,将在程序终止时打印出来。这也是一种指示发生了不可能的事情的方式,比如退出一个无限循环。

http://golang.org/doc/effective_go.html#errors

英文:

The usual way to report an error to a caller is to return an error as an extra return value. The canonical Read method is a well-known instance; it returns a byte count and an error.
But what if the error is unrecoverable? Sometimes the program simply cannot continue.
For this purpose, there is a built-in function panic that in effect creates a run-time error that will stop the program (but see the next section). The function takes a single argument of arbitrary type—often a string—to be printed as the program dies. It's also a way to indicate that something impossible has happened, such as exiting an infinite loop.

http://golang.org/doc/effective_go.html#errors

huangapple
  • 本文由 发表于 2013年6月7日 15:14:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/16978316.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定