当前位置:网站首页>Panic and Recover
Panic and Recover
2022-04-21 06:37:00 【熊小憨】
Panic
说明
程序运行时,发生不应该发生的错误,panic可被用于处理这些错误。或者用于处理那些不准备优雅地处理的错误。
使用
println("before panic")
panic("a problem")
println("after panic")
before panic
panic: a problemgoroutine 1 [running]:
main.main()
文件名:行号 +0x45
从中可以看到,执行到panic时,程序退出,命令行中留下错误信息。
通常情况下,panic应配合error一起使用。
_, err := os.Create("/tmp/file")
if err != nil {
panic(err)
}
Recover
说明
recover功能可用于panic中止程序时,回复程序,使其继续执行。
使用
defer func() {
println("before recover")
if r := recover(); r != nil {
fmt.Println("recovered, error:", r)
}
println("after recover")
}()
println("before panic")
panic("a problem")
println("after panic")
before panic
before recover
recovered, error: a problem
after recover
recover必须在延迟函数中调用。当封闭函数崩溃时,延迟将被激活,recover将捕获panic。
版权声明
本文为[熊小憨]所创,转载请带上原文链接,感谢
https://blog.csdn.net/suma110/article/details/123937388
边栏推荐
- 2022-1-3至2022-1-16 访问者模式
- NP, OSPF link state database
- 交换技术....
- 论文阅读:Security Challenges in an Increasingly Tangled Web
- 【WPF】利用Ellipse或Rectangle制作圆形进度条
- 論文閱讀:Measuring the Impact of a Successful DDoS Attack on the Customer Behaviour of Managed DNS Servi
- PLSQL14软件包下载、汉化、注册
- Leetcode 1557.可以到达所有点的最少点数目(Minimum Number of Vertices to Reach All Nodes)
- Slice upload solves the problem of single file upload
- Database requirement analysis of online ordering system
猜你喜欢

MySQL数据库运行代码后,中文显示的是问号 ?

NP and OSPF default routes

BGP authentication function

IGMP_ Huawei

Common weak passwords in network security devices

NP, OSPF stub area

Ruiyuan power chip, ry3715, ry3750 replacement: silijie sy7208, sy7152, Xinpeng micro ap2008 Core source mp1542, mp3213. Input voltage from 2.5V to 5.5V

NP and OSPF monitoring and commissioning

论文阅读:Measuring the Global Recursive DNS Infrastructure: A View From the Edge

OSPF multi area
随机推荐
404 problem in idea configuration servlet solution
XML编码解码
2020-12-24
2020-12-24
中介者模式(3.28-4.3)
Solving 0 / 1 knapsack problem by dynamic programming
【WPF】数据模板选择器DataTemplateSelector
【WPF】Popup
2022.2.14-2.27 责任链模式
【WPF】转换器Converter
Detailed explanation of the whole evolution process and architecture design of large websites
Guanghuaxin audio codec Daquan cjc4344, cjc8988, cjc5340, cjc6811 guanghuaxin audio codec, cjc4344, cjc8988, cs5340
Switching technology
NP、NSSA区域
Time and Duration and Epoch
synchronized的底层的三种上锁实现方式:(3.21-3.27)
服务器和客户端的套接字通信
Nmap scanning and scapy projects
状态模式(4.4-4.10)
Number