Can Golang Defer Functions Trigger New Panics Without Terminating the Panic Sequence?

Barbara Streisand
Release: 2024-11-03 07:08:29
Original
362 people have browsed it

Can Golang Defer Functions Trigger New Panics Without Terminating the Panic Sequence?

Is Panicking Inside a Defer Function Acceptable, Especially During an Existing Panic?

Consider the following code snippet:

func sub(){
    defer func (){
        panic(2)
    }()
    panic(1)
}

func main(){
    defer func(){
        x:=recover()
        println(x.(int));
    }()
    sub()
}
Copy after login

When executing this code, it appears that the initial panic(1) is superseded by the second panic(2).

Is it permissible to proceed in this manner, or to invoke a Golang function that could panic within a defer function?

Yes, it is acceptable. Panicking from a defer function is not a new or unique situation; it simply indicates that the panic sequence will continue.

The example code provided demonstrates this acceptability and illustrates how even a panic() called from a defer function can be caught by a "higher" level call to recover().

The Golang specification states that if a panic occurs during the execution of deferred functions, the panic sequence will not end if the recovery function executed a panic() without catching it.

Furthermore, all other deferred functions will be executed even if panic() is called within a defer function. However, a panic() without a recovery function in a defer function will "wrap" the existing panic rather than "overwriting" it.

For instance, consider the following code:

func main() {
    defer func() {
        fmt.Println("Checkpoint 1")
        panic(1)
    }()
    defer func() {
        fmt.Println("Checkpoint 2")
        panic(2)
    }()
    panic(999)
}
Copy after login

Output:

Checkpoint 2
Checkpoint 1
panic: 999
    panic: 2
    panic: 1
Copy after login

Even though every defer function calls panic(), they are all still executed, and the resulting panic sequence displays the values passed to each panic() call.

When recover() is called within the defer functions, the final printout also displays this "recovered" information:

defer func() {
    recover()
    fmt.Println("Checkpoint 1")
    panic(1)
}()
defer func() {
    recover()
    fmt.Println("Checkpoint 2")
    panic(2)
}()
Copy after login

Output:

Checkpoint 2
Checkpoint 1
panic: 999 [recovered]
    panic: 2 [recovered]
    panic: 1
Copy after login

The above is the detailed content of Can Golang Defer Functions Trigger New Panics Without Terminating the Panic Sequence?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!