Ding-Log
  • Ding Blog
  • Life
    • My Tool Box
  • Golang
    • Basic
      • Go语言学习笔记
        • Go语言 Channel
        • Go语言 goroutine
        • Go语言 如何写Godoc
        • Go语言panic, recover
        • Go语言的Defer函数
        • Go语言的闭包
        • Go语言接口(Golang - Interface)
        • Go语言的面向对象
      • Go语言爬虫
        • Go语言爬虫 - 广度优先算法
      • Map
    • Web
      • Example to handle GET and POST request in Golang
    • Exercism
      • Isogram
      • ETL
      • Accumulate
      • Scrabble Score
  • SAP
    • SAPUI5&Fiori
      • How to prepare C_FIORDEV_20 SAP Certified Development Associate — SAP Fiori Application Developer
      • SAPUI5 sap.ui.define and sap.ui.require
      • SAPUI5 Models and Binding
      • SAPUI5 MVC Walkthrough
    • ABAP
      • Working with Internal Tables
      • 关于使用内部表的时候的一些注意事项
      • Error Handling
  • Log
Powered by GitBook
On this page
  • panic
  • recover

Was this helpful?

  1. Golang
  2. Basic
  3. Go语言学习笔记

Go语言panic, recover

panic

  • 停止当前函数执行

  • 一直向上返回,执行每一层的defer

  • 如果没有遇见recover,程序退出

recover

  • 仅在defer中调用

  • 获取panic的值

  • 如果无法处理,可以重新panic

package main

import (
	"fmt"
)

func tryRecover() {
	defer func() {
		r := recover()
		if r == nil {
			fmt.Println("Nothing to recover. " +
				"Please try uncomment errors " +
				"below.")
			return
		}
		if err, ok := r.(error); ok {
			fmt.Println("Error occurred:", err)
		} else {
			panic(fmt.Sprintf(
				"I don't know what to do: %v", r))
		}
	}()
	panic(errors.New("this is an error"))

func main() {
	tryRecover()
}
Error occurred: this is an error

系统的panic也可以捕捉到。

b := 0
a := 5 / b
fmt.Println(a)
runtime error: integer divide by zero

但是panic()的参数是interface{},可以传入任何值,而recover()的返回值也是interface{},所以recover不知道如何处理的时候需要继续panic处理。

加入传入一个panic(123)

panic: 123 [recovered]
        panic: I don't know what to do: 123

goroutine 1 [running]:
main.tryRecover.func1()
        /Users/ding/go/coding-180/lang/errhandling/recover/recover.go:19 +0x1e0
panic(0x10a2ca0, 0x10d8850)
        /usr/local/Cellar/go/1.11.4/libexec/src/runtime/panic.go:513 +0x1b9
main.tryRecover()
        /Users/ding/go/coding-180/lang/errhandling/recover/recover.go:35 +0x55
main.main()
        /Users/ding/go/coding-180/lang/errhandling/recover/recover.go:39 +0x20
exit status 2

PreviousGo语言 如何写GodocNextGo语言的Defer函数

Last updated 5 years ago

Was this helpful?