go语言panic&recover

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

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

查看所有标签

猜你喜欢:

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

Practical Algorithms for Programmers

Practical Algorithms for Programmers

Andrew Binstock、John Rex / Addison-Wesley Professional / 1995-06-29 / USD 39.99

Most algorithm books today are either academic textbooks or rehashes of the same tired set of algorithms. Practical Algorithms for Programmers is the first book to give complete code implementations o......一起来看看 《Practical Algorithms for Programmers》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

随机密码生成器
随机密码生成器

多种字符组合密码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具