Can Defer Statements Be Used to Manipulate Return Values in Panicked Functions?

Mary-Kate Olsen
Release: 2024-11-12 17:18:01
Original
764 people have browsed it

Can Defer Statements Be Used to Manipulate Return Values in Panicked Functions?

Manipulating Return Values with Defer

In Go, the defer statement can be used to execute a function after the surrounding function returns. This mechanism allows you to handle cleanup tasks or perform actions even if an error occurs. However, there is a limitation when attempting to return an error value from a function that panics and is enclosed in a defer statement.

Consider the following code snippet:

func getReport(filename string) (rep report, err error) {
    rep.data = make(map[string]float64)

    defer func() {
        if r := recover(); r != nil {
            fmt.Println("Recovered in f", r)
            err, _ = r.(error)
            return nil, err
        }
    }()
    panic("Report format not recognized.")
    // Remaining function code...
}
Copy after login

The intention is to return an error if the getReport function panics. However, this approach doesn't work because defer functions cannot change the number of return values in the surrounding function. Instead, they can only modify the values of the existing return parameters.

To address this issue, the defer function should modify the err return parameter instead of trying to return a new error value:

defer func() {
    if r := recover(); r != nil {
        fmt.Println("Recovered in f", r)
        // Find out the exact error type and set err
        switch x := r.(type) {
        case string:
            err = errors.New(x)
        case error:
            err = x
        default:
            err = errors.New("Unknown panic")
        }
        // Invalidate rep
        rep = nil
    }
}()
Copy after login

With this modification, the defer function updates the err return parameter and sets rep to nil to indicate an error condition. This allows the surrounding function to return the modified err value.

The above is the detailed content of Can Defer Statements Be Used to Manipulate Return Values in Panicked Functions?. 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