Home > Backend Development > Golang > How Can I Recover from Panics in Go Goroutines?

How Can I Recover from Panics in Go Goroutines?

DDD
Release: 2024-12-13 09:01:10
Original
532 people have browsed it

How Can I Recover from Panics in Go Goroutines?

Generic Panic Recovering in Go Programs

When working with Go routines, there may be instances where you want to catch and handle crashes or panics that occur within them, potentially for reporting to an error-reporting server like Sentry or Raygun. However, it's crucial to note that a goroutine cannot recover from a panic in another goroutine.

Idiomatic Approach

The idiomatic way to handle this is to inject code into the function that is launched as a goroutine. This involves utilizing a deferred function that calls recover(), which provides the ability to recover from a panicking state.

Consider the following example:

go func() {
    defer func() {
        if r := recover(); r != nil {
            fmt.Println("Caught:", r)
        }
    }()

    panic("catch me")
}()
Copy after login

This code will output the relevant panic message when executed.

While it's impractical to implement this approach in every goroutine you launch, you can simplify the process by defining a named function that includes the recovery functionality, and call that function within your goroutine (with a deferred call).

func logger() {
    if r := recover(); r != nil {
        fmt.Println("Caught:", r)
    }
}
Copy after login

By calling this function within a deferred closure, you can handle panic situations in a concise manner.

go func() {
    defer logger()
    panic("catch me")
}()
Copy after login

Wrapper Function

For greater convenience, you can create a wrapper function that takes the actual function as input and manages the panic recovery process. This wrapper function can be utilized as follows:

func wrap(f func()) {
    defer func() {
        if r := recover(); r != nil {
            fmt.Println("Caught:", r)
        }
    }()

    f()
}
Copy after login

Using this wrapper simplifies the panic handling process even further:

go wrap(func() {
    panic("catch me")
})
Copy after login

Additional Considerations

The example codes provided demonstrate situations where a goroutine is launched within the wrap() function. This allows the caller to determine whether a new goroutine is necessary by preceding the wrap() call with the go keyword. However, the wrap() function can also be used to protect the execution of arbitrary functions that are passed as input, even if you don't want to run them concurrently in a new goroutine.

The above is the detailed content of How Can I Recover from Panics in Go Goroutines?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template