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 ")// 达不到
}

 

posted on 2015-04-27 11:35  rojas  阅读(361)  评论(0编辑  收藏  举报