go语言panic&recover

Beginner18 · · 1139 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

//PANIC
//文件目录:$GOPATH/src/panic/panic.go
/*1 数组访问越界、空指针引起panic异常
2 panic异常,程序中断运行,并立即执行goroutine
    中defer函数,随后程序崩溃输出日志信息: panic
    value、函数调用堆栈信息;
    panic value通常为某种错误信息,日志信息提供足够诊断工具;
    panic异常与日志信息一并记录至报告;
3 输入值为空接口:
    func panic(v interface{})
4 recover输出值为空接口:
    func recover() interface{}
5 panic及recover英文解释
    5.1 When panic is called, it immediately stops execution of the
    //panic异常,程序回滚goroutine的栈,执行栈中defer函数
    current function and begins unwinding the stack of the goroutine,
    running any deferred functions along the way.
    //若回滚至栈顶,则程序(goroutine)死掉
    If that unwinding reaches the top of the goroutine's stack,
    the program dies.
    //recover函数可以重新控制goroutine,并重回正常执行顺序
    5.2 However, it is possible to use the built-in function recover
    to regain control of the goroutine and resume normal execution.
    //recover只能放置defer函数中,因为panic会回滚defer函数
    A call to recover stops the unwinding and returns the
    argument passed to panic. Because the only code that runs
    while unwinding is inside deferred functions,
    recover is only useful inside deferred functions.
    5.3 recover可用于关闭失败的goroutine而不影响其他goroutine
    One application of recover is to shut down a failing goroutine
    inside a server without killing the other executing goroutines.
*/
package main

import (
    "fmt"
    "os"
    "runtime"
)
func main() {
    //defer函数后panic不会输出栈信息
    defer func() {
        p := recover()
        if p != nil {
            fmt.Println("panic value: ", p)
        }
    }()
    //添加打印栈信息函数可输出栈信息
    defer printStack()
    f(3)
}
func f(x int) {
    fmt.Printf("f(%d)\n", x+0/x) // panics if x == 0
    defer fmt.Printf("defer %d\n", x)
    f(x - 1)
}
func printStack() {
    var buf [4096]byte
    n := runtime.Stack(buf[:], false)
    os.Stdout.Write(buf[:n])
}


//RECOVER
//文件目录:$GOPATH/src/recover/panicRecv.go
/*1 defer函数调用recover, defer程序正常退出或panic
    执行: defer func(){ if p:= recover(); p != nil{};}()
    1.1 recover使程序从panic恢复,并返回panic value
    1.2 导致panic异常的函数不会继续运行,但正常返回
    1.3 未发生panic时调用recover,recover会返回nil
2 对panic value做特殊标记,当recover收到的p值为标记
    值则处理,其他情况继续异常:
    defer func(){
        switch p := recover(); p{
        //无panic异常
        case nil:
        //panic value为标记值,执行recover恢复+处理语句
        case bailout{}:
                err := fmt.Errorf("the error is ...")
        //panic异常且不是标记值继续panic
        default:
                panic(p)
        }
    }()

*/
package main

import "fmt"

func t1(i int) (j int) {
    defer func() {
        if p := recover(); p != nil {
            i = 1 - i
            j = 1
        }
    }()
    if i <= 0 {
        panic("please input a int >0")
    }
    j = i
    return j
    //return uint(i)
}
func main() {
    fmt.Println(t1(-1))
}

 


有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1139 次点击  ∙  1 赞  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传