当前位置:网站首页>golang中的异常处理和异常使用
golang中的异常处理和异常使用
2022-06-11 16:35:00 【EntyIU】
1.go语言中没有其他语言中的try/catch机制
2.在go语言中可以使用panic来抛出异常,使用recover方法来处理异常,通常recover方法写在每个方法defer模块中,在方法return前执行处理
func panic(n interface{})/入参是个空接口意味着可以传递任意类型
func recover() interface{}//反参是空接口意味着可以返回任意类型3.go语言中的异常接口
type error interface{
Error()
}如果要使用创建该接口的实例只需要使用结构体实现该接口的Error的方法即可,go中内置了创建接口实例的方法
errors.new("异常信息")//返回的是error接口实例,接口中方法重写Error方法返回的是,字符串异常信息
fmt.Errorf()4.开发中的常用技巧
type error struct {
code int
message string
}
func (e error) Error() string {
return fmt.Sprintf("error:", e.code)
}
func main() {
defer func() {
i := recover()
e, ok := i.(error)
if ok {
if e.code == 1 {
fmt.Println(e.message)
}
}
}()
panic(error{1, "FGDDGFGD"})边栏推荐
- 网络流媒体协议的联系与区别(RTP RTCP RTSP RTMP HLS)
- Switching power supply circuit diagram and principle 12V analysis - detailed version
- jsp页面初始加载方式
- 药物评价指标
- 【opencvsharp】opencvsharp_samples.core示例代码笔记
- 什么是rs邮票纸?
- Search and graph theory: Dijkstra finding the shortest path i-dijkstra (shortest path)
- 关联关系
- Differences between list and set access elements
- 2022安全员-A证考试题模拟考试题库模拟考试平台操作
猜你喜欢

2022 R1 quick opening pressure vessel operation test question bank and simulation test

2022安全员-C证特种作业证考试题库及答案

Text driven for creating and editing images (with source code)

A team of heavyweights came to the "digital transformation" arena of CLP Jinxin ice and snow sports

2022安全员-A证考试题模拟考试题库模拟考试平台操作

tornado环境搭建及基本框架搭建——熟悉的hello world

1267_FreeRTOS启动第一个任务接口prvPortStartFirstTask实现分析

Solve the problem that jupyter cannot connect to the kernel based on pycharm and Anaconda -- solution 1

laravel 监听模式

项目经理如何击退被工作汇报支配的恐惧感?
随机推荐
Leetcode 1974. 使用特殊打字机键入单词的最少时间(可以,一次过)
(OJ assignment of Hunan University of science and Technology) problem g: pattern matching of strings
消息队列-推/拉模式学习 & ActiveMQ及JMS学习
2022高压电工特种作业证考试题库及在线模拟考试
C语言各数据类型的内存映像
Web page design example assignment -- Introduction to Henan cuisine (4 pages) web final assignment design web page_ Dessert and gourmet college students' web design homework finished product
unittest 如何知道每个测试用例的执行时间
什么是rs邮票纸?
2022年R1快开门式压力容器操作考试题库及模拟考试
瑞吉外卖项目(三)员工管理业务开发
[sword finger offer] 21 Adjust array order so that odd numbers precede even numbers
Wechat applet timestamp conversion time format + time subtraction
学生网站模板棕色蛋糕甜品网站设计——棕色蛋糕甜品店(4页) 美食甜品网页制作期末大作业成品_生鲜水果网页设计期末作业
大龄码农从北京到荷兰的躺平生活
solr(一)solr的安装及权限控制
2022 safety officer-a certificate test question simulation test question bank simulation test platform operation
Oracle 分析函数 over 和MySQL 实现类似效果写法
leetcode785. 判断二分图(中等)
RSP:遥感预训练的实证研究
What is RS stamp paper?