golang recover panic 流程控制的可达与不可达

--------------------------流程控制可达-----------------------------

package main



import "fmt"



func explode() {

    // Cause a panic.

    panic("WRONG")



}



func throwPanic(f func()) {

    defer func() {

        if err := recover(); err != nil {

            // Handle our error.

            fmt.Println("FIX")

            fmt.Println("ERR", err)

        }

    }()

    f()

    fmt.Println(" finish")

}



func main() {

    throwPanic(explode)

    fmt.Println(" reach here ")//可达

}







--------------------------流程控制不可达-----------------------------

package main



import "fmt"



func explode() {

    // Cause a panic.

    panic("WRONG")

}



func main() {

    // Handle errors in defer func with recover.

    defer func() {

    if err := recover(); err != nil {

        // Handle our error.

        fmt.Println("FIX")

        fmt.Println("ERR", err)

    }

    }()

    // This causes an error.

    explode()

    fmt.Println(" reach here ")// 达不到

}

 

你可能感兴趣的:(golang)