golang panic recoverrecover 找出哪一行

Golang(147)
package main
func main() {
fmt.Println(&Returned normally from f.&)
func f() {
defer func() {
if r := recover(); r != nil {
fmt.Println(&Recovered in f&, r)
fmt.Println(&Recovered in f&, r)
fmt.Println(&Calling g.&)
fmt.Println(&Returned normally from g.&)
func g() {
panic(&ERROR&)
panic 意思是抛出一个异常, 和python的raise用法类似
recover是捕获异常,和python的except用法类似
defer会延迟函数到其他函数之后完之后再执行,后面跟的是函数
golang 的错误处理流程:当一个函数在执行过程中出现了异常或遇到
panic(),正常语句就会立即终止,然后执行 defer 语句,再报告异
常信息,最后退出 goroutine。如果在 defer 中使用了 recover()
函数,则会捕获错误信息,使该错误信息终止报告。
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:140450次
积分:2973
积分:2973
排名:第11666名
原创:153篇
评论:29条
(2)(2)(5)(1)(3)(1)(2)(4)(3)(2)(5)(6)(5)(1)(4)(7)(11)(3)(8)(5)(2)(6)(9)(15)(12)(11)(20)(1)go语言学习(2)
在go语言中,panic和recover是何方神圣?
详细参见:https://golang.org/pkg/builtin/
func panic(v interface{})
The panic built-in function stops normal execution of the current goroutine. When a function F calls panic, normal execution of F stops immediately. Any functions whose execution was deferred by F are run in the usual way, and then F returns to its caller.
To the caller G, the invocation of F then behaves like a call to panic, terminating G's execution and running any deferred functions. This continues until all functions in the executing goroutine have stopped, in reverse order. At that point, the program is
terminated and the error condition is reported, including the value of the argument to pa. This termination sequence is called panicking and can be controlled by the built-in function recover.
func recover() interface{}
The recover built-in function allows a program to manage behavior of a panicking goroutine. Executing a call to recover inside a deferred function (but not any function called by it) stops the panicking sequence by restoring normal execution and retrieves the
error value passed to the call of panic. If recover is called outside the deferred function it will not stop a panicking sequence. In this case, or when the goroutine is not panicking, or if the argument supplied to panic was nil, recover returns nil. Thus
the return value from recover reports whether the goroutine is panicking.
1、内建函数
2、假如函数F中书写了panic语句,会终止其后要执行的代码,在panic所在函数F内如果存在要执行的defer函数列表,按照defer的逆序执行
3、返回函数F的调用者G,在G中,调用函数F语句之后的代码不会执行,假如函数G中存在要执行的defer函数列表,按照defer的逆序执行
4、直到goroutine整个退出,并报告错误
1、内建函数
2、用来控制一个goroutine的panicking行为,捕获panic,从而影响应用的行为
3、一般的调用建议
& & a). 在defer函数中,通过recever来终止一个gojroutine的panicking过程,从而恢复正常代码的执行
& & b). 可以获取通过panic传递的error
调用函数定义:
被调用函数定义:
调用代码:
从运行结果可以看出:
1、当遇到panic,panic之后的语句是不会执行的 “I 'm children, I 'm eating”
2、被调用函数 “Test_TheCallee_F_toverify_panic_and_recover_func” 中的所有defer定义都会按照逆序执行,才会返回到调用函数。不管被调用函数中任意一个defer中是否使用recover处理了panic
3、调用函数 “Test_Caller_G” 在被调用函数返回后,是否会继续向下执行代码,取决于被调用函数是否处理了panic还是继续执行panic
4、例子中,由于被调用函数 ”Test_TheCallee_F_toverify_panic_and_recover_func“ 的 某个defer “deferOfTheCallee”的定义中处理了panic(只是答应message到屏幕),并没有继续执行panic来触发panicking,所以调用函数执行完后,仍然可以继续向下处理代码
通过构建panic的时候,传入非error类型,来继续触发panic,我们来看看输出结果:
修改代码(panic只传递一个字符串,非error类型):
修改后的执行结果:
最终的结论:
1、panic在程序中出现,如果不使用recover在defer中处理,goroutine最终回退出。退出的整个流程详细参见panic的官方说明文档介绍
2、recover建议只写在defer中,可以提供一个通用的defer,在很多需要处理错误的方法内部,最终都会执行这个defer
3、在这个通用defer的逻辑内部,通过recover来处理是否继续将错误panic,还是只是处理错误。具体的做法可以参见如下文章:
a).&/question/
b).&/reusee/codes/blob/master/err/err.go
c).&/reusee/socks5hs/blob/master/socks5hs.go#L37
关于如何在代码中避免到处使用&
if err != nil {
& & return err
可以参见go的官方推荐方法:
https://blog.golang.org/errors-are-values
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:50807次
排名:千里之外
原创:34篇
(1)(1)(1)(1)(1)(2)(9)(5)(6)(2)(2)(2)(3)(1)Posted by 0x55aa
在看《build web application with golang》地址:/astaxie/build-web-application-with-golang
看到Panic和Recover这一节,对Panic和Recover有了了解。
Go没有例如像Java那样的异常机制:不能抛出一个异常。作为代替,它使用了panic和recover机制。一定要记得,这应当作为最后的手段被使用,你的代码中应当没有,或者很少的令人恐慌的东西。这是个强大的工具,明智的使用它。那么,应该如何使用它。
Panic是一个内建函数,可以中断原有的控制流程,进入一个令人恐慌的流程中。当函数F调用panic,函数F的执行被中断,但是F中的延迟函数会正常执 行,然后F返回到调用它的地方。在调用的地方,F的行为就像调用了panic。这一过程继续向上,直到发生panic的goroutine中所有调用的函 数返回,此时程序退出。恐慌可以直接调用panic产生。也可以由运行时错误产生,例如访问越界的数组。Recover是一个内建的函数,可以让进入令人恐慌的流程中的goroutine恢复过来。Recover仅在延迟函数中有效。在正常的执行过程中,调用 recover会返回nil,并且没有其他任何效果。如果当前的goroutine
陷入恐慌,调用recover可以捕获到panic的输入值,并且恢复正常的执行。最容易理解就是给个例子,文章里有例子:package main
var user = ""func inita() {
defer func(){
fmt.Print("defer##\n")
if user == "" {
fmt.Print("@@@before panic\n")
panic("no value for user\n")
fmt.Print("!!after panic\n")
func throwsPanic (f func()) (b bool){
defer func(){
if x:= recover(); x != nil{
fmt.Print(x)
fmt.Print("after the func run")
func main(){
throwsPanic(inita)}执行结果:D:\go&go run b.go@@@before panicdefer##no value for user
如上面所说的:panic在user=""时,打断了函数的执行,fmt.Print("!!after panic\n")没有执行。但函数中的延迟函数会正常执行,打印了 ”defer##“。然后返回到调用该函数的地方,继续上面的过程。直到执行完所有函数的defer,退出程序。Recover可以捕获到panic的值,上面的打印“no value for user”。并且恢复正常的执行。
相关文章:panic golang异常panic和恢复recover用法 - 为程序员服务
为程序员服务
golang异常panic和恢复recover用法
package main
import &fmt&
func main() {
defer func() {
//必须要先声明defer,否则不能捕获到panic异常
fmt.Println(&c&)
if err := recover(); err != nil {
fmt.Println(err)
//这里的err其实就是panic传入的内容,55
fmt.Println(&d&)
func f() {
fmt.Println(&a&)
fmt.Println(&b&)
fmt.Println(&f&)
您可能的代码
相关聚客文章
相关专栏文章问题对人有帮助,内容完整,我也想知道答案
问题没有实际价值,缺少关键内容,没有改进余地
go fasthttp由于使用了“线程池”的机制对于效率有某些方面的提示。但是 一旦出现panic 不处理的话,整个线程池就都崩溃了引起网站的整体退出,各位大大是怎么使用fasthttp的呀?每个handler加recover么?
同步到新浪微博
分享到微博?
Hi,欢迎来到 SegmentFault 技术社区!⊙▽⊙ 在这里,你可以提出编程相关的疑惑,关注感兴趣的问题,对认可的回答投赞同票;大家会帮你解决编程的问题,和你探讨技术更新,为你的回答投上赞同票。
明天提醒我
关闭理由:
删除理由:
忽略理由:
推广(招聘、广告、SEO 等)方面的内容
与已有问题重复(请编辑该提问指向已有相同问题)
答非所问,不符合答题要求
宜作评论而非答案
带有人身攻击、辱骂、仇恨等违反条款的内容
无法获得确切结果的问题
非开发直接相关的问题
非技术提问的讨论型问题
其他原因(请补充说明)
我要该,理由是:

我要回帖

更多关于 golang 命令行参数 的文章

 

随机推荐