Home > Backend Development > Golang > How Do Panics Behave in Deferred Functions in Go?

How Do Panics Behave in Deferred Functions in Go?

DDD
Release: 2024-11-02 08:28:29
Original
697 people have browsed it

How Do Panics Behave in Deferred Functions in Go?

Panic Handling in Deferred Functions in Go

Panic handling in Go is a crucial aspect of error management. While it's commonly discouraged to panic from a destructor in C , it's safe and acceptable to panic from within a deferred function in Go.

Panic Propagation and Overwriting

In the provided code example:

<br>func sub(){</p>
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">defer func (){
    panic(2)
}()
panic(1)
Copy after login

}

func main(){

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

}

The second panic(2) does not overwrite the first panic(1). Instead, both panics are propagated to the main function, where the deferred recovery function retrieves and prints the value from the first panic(1).

Panic Termination and Recovery

When a panic occurs within a deferred function, the panic sequence continues. However, if the deferred function recovers from the panic (with the recover() function), the panic sequence stops. This ensures that panics do not terminate the Go program prematurely.

Example: Multiple Deferred Panics and Recovery

As demonstrated in the following example:

<br>func main() {</p>
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">defer func() {
    fmt.Println("Checkpoint 1")
    panic(1)
}()
defer func() {
    fmt.Println("Checkpoint 2")
    panic(2)
}()
panic(999)
Copy after login

}

Even though all deferred functions execute panic(), recovery is possible from any deferred function, as evident in the following modified example:

<br>defer func() {</p>
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">recover()
fmt.Println("Checkpoint 1")
panic(1)
Copy after login

}()
defer func() {

recover()
fmt.Println("Checkpoint 2")
panic(2)
Copy after login

}()

In this case, the final panic sequence includes information about all panic values and indicates where recovery occurred.

Conclusion

Panic handling in deferred functions in Go provides flexibility and allows for the continuation of panicking sequences. The ability to recover from a deferred-panic ensures that panics do not prematurely terminate the program. Go programmers can leverage this mechanism to implement robust error handling strategies.

The above is the detailed content of How Do Panics Behave in Deferred Functions in Go?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Previous article:Can You Panic Inside a Defer Function During an Existing Panic in Go? Next article:How to Fix the \"import object is expected\" Error in Golang 1.4.2?
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
Latest Issues
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template