码迷,mamicode.com
首页 > 其他好文 > 详细

defer, panic, recover使用总结

时间:2019-05-26 16:14:53      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:code   main   info   import   fun   imp   over   使用   代码   

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结合使用:panic触发错误,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")
}

技术图片

 

defer, panic, recover使用总结

标签:code   main   info   import   fun   imp   over   使用   代码   

原文地址:https://www.cnblogs.com/BluePegasus/p/10926175.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!