当前位置:网站首页>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 :
- 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 ;
- 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 ;
- 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
- recover To capture panic, To resume normal code execution ;
- recover Must cooperate defer Use ;
- 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 .
边栏推荐
猜你喜欢

GO语言-自定义error

(IntelliJ)插件一 Background Image Plus

Hcip fifth operation

pkav简单爆破

Jetpack Compose 从开门到入门之 MenuBar桌面菜单(Desktop Menu)

Three implementation methods: left fixed and right adaptive (Flex, float + BFC, float margin left)

渗透测试基础 | 附带测试点、测试场景

C'est dur de trouver un emploi? Ali des trois côtés, heureusement qu'il s'est bien préparé et qu'il a pris un produit.

Complete one-time GC process of JVM principle

物联网开源开发平台 Shifu 开放内测!第一版技术文档发布
随机推荐
1010 Radix
[leetcode] longest increasing subsequence problem and its application
【opencv450】帧间差分法
奇门遁甲辅助决策软件
渗透测试基础 | 附带测试点、测试场景
insert into... Where not exists insert to avoid repeated use
计算欧式距离和余弦相似度
MCS: discrete random variable
云原生数据库是未来数据库的天下
Win软件 - (Net-Framework)已处理证书链,但是在不受信任提供程序信任的根证书中终止
Swiftui 2.0 course notes Chapter 5
Raspberry pie network remote access
Open source ecology 𞓜 super practical open source license basic knowledge literacy post (Part 2)
Difficult to find a job in a bad environment? Ali on three sides. Fortunately, he has made full preparations and has offered
今日睡眠质量记录80分
九九乘法表.bat
TIOBE 编程语言排行榜是编程语言流行趋势的一个指标
GO语言-自定义error
BGP summary of hcip operation
104. 简易聊天室7:使用 Socket 传递对象