设为首页 加入收藏

TOP

defer, panic, recover使用总结
2019-05-26 16:14:37 】 浏览:25
Tags:defer panic recover 使用 总结

1. defer : 延迟调用。多个defer,依次入栈,在函数即将退出时,依次出栈调用

 1 package main
 2 import "fmt"
 3 func main() {
 4     defer func() {
 5         fmt.Println("defer one")   
 6     }()
 7     defer func() {
 8         fmt.Println("defer two")   
 9     }()
10     defer func() {
11         fmt.Println("defer three")  
12     }()
13 }

2. panic和defer结合使用:pani

c触发错误,defer依次出栈调用,没有recover捕获的情况下,最后才打印错误

 1 package main
 2 import "fmt"
 3 func main() {
 4     defer func() {
 5         fmt.Println("defer one")
 6     }()
 7     defer func() {
 8         fmt.Println("defer two")
 9     }()
10     defer func() {
11         fmt.Println("defer three")
12     }()
13     panic("panic here")
14 }

3. defer,panic, recover 结合使用,panic触发错误,defer依次出栈调用,直到被recover捕获,打印捕获的信息,之后继续defer出栈

例一:

 1 package main
 2 import "fmt"
 3 func main() {
 4     defer func() {
 5         fmt.Println("defer one")
 6     }()
 7     defer func() {
 8         fmt.Println("defer two")
 9     }()
10     defer func() {
11         if info := recover(); info != nil {
12             fmt.Println("catch: ", info)
13         }
14         fmt.Println("defer three")
15     }()
16     panic("panic here")
17 }

例二:

package main
import "fmt"
func main() {
    defer func() {
        fmt.Println("defer one")
    }()
    defer func() {
        if info := recover(); info != nil {
            fmt.Println("catch: ", info)
        }
        fmt.Println("defer two")
    }()
    defer func() {
        fmt.Println("defer three")
    }()
    panic("panic here")
}

例三:

package main
import "fmt"
func main() {
    defer func() {
        if info := recover(); info != nil {
            fmt.Println("catch: ", info)
        }
        fmt.Println("defer one")
    }()
    defer func() {
        fmt.Println("defer two")
    }()
    defer func() {
        fmt.Println("defer three")
    }()
    panic("panic here")
}

4. recover 必须在defer中调用,才有效,否则返回nil

package main
import "fmt"
func main() {
    if info := recover(); info != nil {
        fmt.Println("catch: ", info)
    } else {
        fmt.Println("recover return nil")
    }
    defer func() {
        fmt.Println("defer one")
    }()
    defer func() {
        fmt.Println("defer two")
    }()
    defer func() {
        fmt.Println("defer three")
    }()
    panic("panic here")
}

5. panic 其后的代码不会执行

例一:

package main
import "fmt"
func main() {
    defer func() {
        fmt.Println("defer one")
    }()
    defer func() {
        fmt.Println("defer two")
    }()
    defer func() {
        fmt.Println("defer three")
    }()
    panic("panic here")
    if info := recover(); info != nil {
        fmt.Println("catch: ", info)
    } else {
        fmt.Println("recover return nil")
    }
}

例二:

package main
import "fmt"
func main() {
    defer func() {
        fmt.Println("defer one")
    }()
    defer func() {
        if v := recover(); v != nil {
            fmt.Println("catch panic error: ", v)
        }
        fmt.Println("defer two")
    }()
    defer func() {
        fmt.Println("defer three")
    }()
    panic("panic here")
    fmt.Println("after panic")
}

 


编程开发网
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇xorm -Exist方法实例 下一篇xorm -Get方法实例

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

array(4) { ["type"]=> int(8) ["message"]=> string(24) "Undefined variable: jobs" ["file"]=> string(32) "/mnt/wp/cppentry/do/bencandy.php" ["line"]=> int(217) }