go recover

go recover运用

package recovery_test

import (
	"fmt"
	"testing"
)


func TriggerPanic() {
	a := []int{1, 2, 3}
	fmt.Println(a[4])
}

func RecoverError() {
	fmt.Println("defer func")
	if err := recover(); err != nil {
		fmt.Println("recover success")
	}
}

func TestRecover(t *testing.T) {
	defer RecoverError()
	TriggerPanic()

	fmt.Println("go on")
}
posted @ 2024-05-17 14:35  Jikefan  阅读(13)  评论(0)    收藏  举报