当前位置:网站首页>Go language -panic and recover

Go language -panic and recover

2022-06-23 05:27:00 Crying while learning

Preface

        go Language pursues simplicity , therefore go Not in the language try...catch sentence . because go The author of the language believes that mixing exceptions with control statements , It's easy to confuse this program , Exceptions can also be easily abused .

         So in go In language , To prevent exceptions from being abused . We often use the return value of a function to return an error , Instead of using exceptions instead of errors .

         If you really need to handle exceptions in some scenarios , You can use panic and recover.panic Used to throw an exception ,recover Used to recover exceptions .

panic

panic Trigger process :

  1. If the function F Writing in and triggering panic sentence , Code to be executed after will be terminated . stay panic Function where F If there is a to be executed defer Function list , According to defer Reverse the writing order ;
  2. If the function G Call function F, The function F panic Then return the caller function G. function G in , Call function F Statements after statements are not executed . Suppose the function G There are also to be implemented defer Function list , According to defer The reverse of the writing order is OK ;
  3. Quit the whole goroutine, And report the error .
func main() {
	defer fmt.Println("main---1---")
	defer fmt.Println("main---2---")
	fmt.Println("main---3---")
	mytest(1)
	defer fmt.Println("main---4---")
	fmt.Println("main---5---")
}

func mytest(num int) {
	defer fmt.Println("mytest---1---")
	defer fmt.Println("mytest---2---")
	fmt.Println("mytest---3---")
	if num == 1 {
		panic(" Something unusual happened , Throw out panic")
	}
	defer fmt.Println("mytest---4---")
	fmt.Println("mytest---5---")
}

  Code execution process :

from main Function into --> encounter main Function 2 strip defer sentence --> Print "main---3---" --> Get into mytest function

--> encounter mytest Function 2 strip defer sentence --> Print "mytest---3---" --> if Statement for true Trigger panic --> Reverse order execution mytest Function panic Before 2 strip defer sentence --> Return external function (main function ) -->  Reverse order execution main Function mytest Before function 2 strip defer sentence --> Throw an exception

because panic Throw an exception , You can see mytest function panic Statements following statements are not executed ; After returning the external function ,mytest The statements after the function are not executed .

recover

  1. recover To capture panic, To resume normal code execution ;
  2. recover Must cooperate defer Use ;
  3. recover No parameters passed in , But there is a return value , The return value is panic Value delivered

recover Example

	defer func() {
		if msg := recover(); msg != nil {
			fmt.Println("panic Information :", msg, "---recover recovery ---")
		}
	}()

panic Examples in examples , If in mytest Add recover take panic After recovery , What is the execution effect and sequence ?

func main() {
	defer fmt.Println("main---1---")
	defer fmt.Println("main---2---")
	fmt.Println("main---3---")
	mytest(1)
	defer fmt.Println("main---4---")
	fmt.Println("main---5---")
}

func mytest(num int) {
	defer func() {
		if msg := recover(); msg != nil {
			fmt.Println("panic Information :", msg, "---recover recovery ---")
		}
	}()
	defer fmt.Println("mytest---1---")
	defer fmt.Println("mytest---2---")
	fmt.Println("mytest---3---")
	if num == 1 {
		panic(" Something unusual happened , Throw out panic")
	}
	defer fmt.Println("mytest---4---")
	fmt.Println("mytest---5---")
}

  Code execution process :

from main Function into --> encounter main Function 2 strip defer sentence --> Print "main---3---" --> Get into mytest function

--> encounter mytest Function 3 strip defer sentence --> Print "mytest---3---" --> if Statement for true Trigger panic --> Reverse order execution mytest Function panic Before 3 strip defer sentence -->  Go to Article 3 defer when ,recover Restore panic and output information --> Return external function (main function ) -->  encounter mytest After function 1 strip defer sentence --> Print "mytest---5---" --> Reverse order execution main Function 3 strip defer sentence

because recover Restore panic , So the program won't just panic And exit execution , therefore mytest After returning the external function , External functions can also be executed normally .

 

原网站

版权声明
本文为[Crying while learning]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230335403161.html