我被提到了这个问题:
它可以正常工作,但依赖于知道恐慌发生在哪里以放置递延功能。
我的代码如下。
package main
import "fmt"
func main() {
defer recoverPanic()
f1()
f2()
f3()
}
func f1() {
fmt.Println("f1")
}
func f2() {
defer f3() //<--- don't want to defer f3 here because I might not know f2 will panic, panic could occuer elsewhere
fmt.Println("f2")
panic("f2")
}
func f3() {
fmt.Println("f3")
}
func recoverPanic() {
if r := recover(); r != nil {
fmt.Printf("Cause of panic ==>> %q\n", r)
}
}
在恐慌函数中,在下面输出递延函数调用f3()。
f1
f2
f3
Cause of panic ==>> "f2"
如果您有一个不知道恐慌发生在哪里的应用程序,我是否需要延期可能会恐慌的功能?
评论 defer f3()
给我以下输出。
f1
f2
Cause of panic ==>> "f2"
F3永远不会运行。
我的问题是如何在可能恐慌的每个函数中继续执行程序,而无需延期函数?
I was referred to this question: Program recovered from panic does not exit as expected
It works fine but it relies on knowing where the panic occurs in order to place the deferred function.
My code is as follows.
package main
import "fmt"
func main() {
defer recoverPanic()
f1()
f2()
f3()
}
func f1() {
fmt.Println("f1")
}
func f2() {
defer f3() //<--- don't want to defer f3 here because I might not know f2 will panic, panic could occuer elsewhere
fmt.Println("f2")
panic("f2")
}
func f3() {
fmt.Println("f3")
}
func recoverPanic() {
if r := recover(); r != nil {
fmt.Printf("Cause of panic ==>> %q\n", r)
}
}
Having the deferred function call f3() in the panicking function works, output below.
f1
f2
f3
Cause of panic ==>> "f2"
What if you have an application where you don't know where a panic occurs, do I need to put a defer in every function that might panic?
Commenting out the defer f3()
gives me the following output.
f1
f2
Cause of panic ==>> "f2"
f3 never runs.
My question is how to continue execution of the program without having a deferred function call in every function that might panic?
发布评论
评论(1)
恐慌后您无法恢复函数执行。当当前执行线无法正确继续时,请使用恐慌。恐慌之后任意恢复执行(如果可能的话)立即乞求另一个恐慌,因为国家已经不正确,而只是向前爆发并无法解决。
例如,当它试图从切片上读取范围时,假设函数恐慌。如何继续?继续继续是什么意思?它应该只读取界限内存位置并获取垃圾数据吗?继续零值?与切片不同?
您必须处理错误案例;通过显式< / em>恢复,或者先发制人检查 /纠正条件,这会导致恐慌。至少在标准库中,可能引起恐慌的功能会在其文档中这样说,并解释哪些条件会导致恐慌。
如果您通常需要安全地调用 void 功能并从任何恐慌中恢复,则可以为此做出一个简单的包装功能。
然后
You can't resume function execution after a panic. Panic is used when the current line of execution cannot continue correctly. Arbitrarily resuming execution after a panic (if it were possible) is begging immediately for another panic, because the state is already incorrect and just blazing ahead won't fix that.
For example, let's say a function panics when it tries to read out of bounds on a slice. How can it continue? What would it even mean to continue? Should it just read the out of bounds memory location and get garbage data? Continue with a zero value? Take a different value from the slice?
You must handle error cases; either by explicitly recovering, or preemptively checking / correcting conditions that will result in panic. At least in the standard library, functions that may spur a panic will say so in their documentation with an explanation of which conditions will result in panic.
If you commonly need to safely call void functions and recover from any panics, you can make a simple wrapper function for that.
Then