go语言panic&recover

栏目: Go · 发布时间: 7年前

//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语言panic&recover》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

C++编程思想(第1卷)

C++编程思想(第1卷)

[美] Bruce Eckel / 刘宗田、袁兆山、潘秋菱 / 机械工业出版社 / 2002-9 / 59.00元

《C++编程思考》第2版与第1版相比,在章节安排上有以下改变。增加了两章:“对象的创建与使用”和“C++中的C”,前者与“对象导言”实际上是第1版“对象的演化”一章的彻底重写,增加了近几年面向对象方法和编程方法的最瓣研究与实践的有效成果,后者的添加使不熟悉C的读者可以直接使用这本书。删去了四章:“输入输出流介绍”、“多重继承”、“异常处理”和“运行时类型识别”,删去的内容属于C++中较复杂的主题,......一起来看看 《C++编程思想(第1卷)》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具