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

查看所有标签

猜你喜欢:

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

代码之髓

代码之髓

[日] 西尾泰和 / 曾一鸣 / 人民邮电出版社 / 2014-8 / 45.00元

《代码之髓:编程语言核心概念》作者从编程语言设计的角度出发,围绕语言中共通或特有的核心概念,通过语言演变过程中的纵向比较和在多门语言中的横向比较,清晰地呈现了程序设计语言中函数、类型、作用域、类、继承等核心知识。本书旨在帮助读者更好地理解各种概念是因何而起,并在此基础上更好地判断为何使用、何时使用及怎样使用。同时,在阅读本书后,读者对今后不断出现的新概念的理解能力也将得到提升。 《代码之髓:......一起来看看 《代码之髓》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具