Go 异常捕获处理: panic(err) 与 recover()
代码实例
func (rcvr *CQL) Compile(ctx context.Context) string {defer func() {if err := recover(); err != nil {//打印调用栈信息buf := make([]byte, 2048)n := runtime.Stack(buf, false)stackInfo := fmt.Sprintf("%s", buf[:n])logu.CtxError(ctx, error_code.ProcessError, "Compile", "[FillStructInfo] rcvr=%v, resultRecord=%v, panic recovered: \n%v\n%v", convert.ToJSONString(rcvr), err, stackInfo)}}()return rcvr.compile1()
}
panic / recover 源码注释
// The panic built-in function stops normal execution of the current
// goroutine. When a function F calls panic, normal execution of F stops
// immediately. Any functions whose execution was deferred by F are run in
// the usual way, and then F returns to its caller. To the caller G, the
// invocation of F then behaves like a call to panic, terminating G's
// execution and running any deferred functions. This continues until all
// functions in the executing goroutine have stopped, in reverse order. At
// that point, the program is terminated with a non-zero exit code. This
// termination sequence is called panicking and can be controlled by the
// built-in function recover.
func panic(v any)// The recover built-in function allows a program to manage behavior of a
// panicking goroutine. Executing a call to recover inside a deferred
// function (but not any function called by it) stops the panicking sequence
// by restoring normal execution and retrieves the error value passed to the
// call of panic. If recover is called outside the deferred function it will
// not stop a panicking sequence. In this case, or when the goroutine is not
// panicking, or if the argument supplied to panic was nil, recover returns
// nil. Thus the return value from recover reports whether the goroutine is
// panicking.
func recover() any
runtime.Stack(buf []byte, all bool) int
// Stack formats a stack trace of the calling goroutine into buf
// and returns the number of bytes written to buf.
// If all is true, Stack formats stack traces of all other goroutines
// into buf after the trace for the current goroutine.
func Stack(buf []byte, all bool) int {if all {stopTheWorld("stack trace")}n := 0if len(buf) > 0 {gp := getg()sp := getcallersp()pc := getcallerpc()systemstack(func() {g0 := getg()// Force traceback=1 to override GOTRACEBACK setting,// so that Stack's results are consistent.// GOTRACEBACK is only about crash dumps.g0.m.traceback = 1g0.writebuf = buf[0:0:len(buf)]goroutineheader(gp)traceback(pc, sp, 0, gp)if all {tracebackothers(gp)}g0.m.traceback = 0n = len(g0.writebuf)g0.writebuf = nil})}if all {startTheWorld()}return n
}
http://www.taodudu.cc/news/show-5012696.html
相关文章:
- 开机出现all boot options are tried 三星笔记本修复方案
- 一看就会一做就废系列:说说 RECOVER DATABASE(上)
- bzip2recover
- TiDB-unsafe recover(三台tikv宕机两台)
- 结合源码和日志分析 mesos agent recover 过程
- 053试题 329 - recover corruption list
- oracle把日志文件全部删除,归档日志文件丢失,数据文件为RECOVER状态如何删除或恢复...
- go defer,panic,recover详解 go 的异常处理
- range form /recover from等动词词组
- 053试题 193 - recover 命令
- Go-defer,panic,recover
- leveldb:数据库recover机制
- 使用Fy_Recover_data恢复被truncate的表
- Golang中panic与recover的实现原理
- oracle 12c recover table恢复单表
- go panic recover 思考
- 数据恢复软件横向评测(转)
- Permute 3 for mac(万能音视频转换器)v3.9.4中文版
- 视频格式转换器免费
- 万能视频转换:实现难搞的监控视频.A64格式转成普通视频
- macOS万能音视频转换器-Permute 3 for mac完整安装-简单易学的使用方法
- Total Audio Converter(万能转换器)
- 高标清双向变换 万能 视频格式转换器
- 管理员已阻止你运行此应用有关详细信息请与管理员联系,怎么办,WIN10
- java计算机毕业设计学生健康信息管理源程序+mysql+系统+lw文档+远程调试
- java计算机毕业设计学生就业创业管理系统源程序+mysql+系统+lw文档+远程调试
- java计算机毕业设计web高校车辆调度系统设计与实现MyBatis+系统+LW文档+源码+调试部署
- java-net-php-python-52学生评奖评优管理系统计算机毕业设计程序
- java毕业生设计一起组局校园交友平台计算机源码+系统+mysql+调试部署+lw
- java毕业生设计学校食堂管理计算机源码+系统+mysql+调试部署+lw
Go 异常捕获处理: panic(err) 与 recover()相关推荐
- go 异常捕获处理 panic defer recover
简言 在其他语言里,宕机往往以异常的形式存在,底层抛出异常,上层逻辑通过 try/catch 机制捕获异常,没有被捕获的严重异常会导致宕机 go语言追求简洁,优雅,Go语言不支持传统的 try-cat ...
- go 异常捕获和处理(panic/recover)
异常处理 Golang 没有结构化异常,使用 panic 抛出错误,recover 捕获错误. 异常的使用场景简单描述:Go中可以抛出一个panic的异常,然后在defer中通过recover捕获这个 ...
- 简单介绍Go语言错误处理异常捕获+异常抛出
这篇文章主要介绍了Go语言错误处理异常捕获和异常抛出,Go语言的作者认为java等语言的错误处理底层实现较为复杂,就实现了函数可以返回错误类型以及简单的异常捕获,虽然简单但是也非常精妙,大大的提高了运 ...
- Intel Realsense D435 当摄像头运行过程中突然USB线断开,对RuntimeError: Frame didn't arrived within 5000的异常捕获及处理
如图,在摄像头运行过程中,摄像头突然断开,可能设备需要对异常进行捕获并处理(如摄像头重连,发出警报,发送信号给车辆让它停止前进等) 需阅读,python异常捕获及处理 191225 通过捕获所有异常, ...
- 《SpringBoot从菜鸟到老鸟》之SpringBoot 如何配置全局的异常捕获
SpringBoot 如何配置全局的异常捕获 SpringBoot中自带的异常捕获机制返回的默认页面比较丑,对用户来说不够人性化. 所以这篇文章来讲解SpringBoot钟自定义全局异常捕获. 主要讲 ...
- 【C语言基础】C语言异常捕获机制 - setjmp
C语言异常捕获机制 - setjmp 快速入门 想快速入门该模块请访问:介绍,数据接口,示例代码 介绍 C语言没有C++或Java的异常捕获机制,但可以通过setjmp/longjmp实现类似的效果: ...
- js 基础 -- 循环、函数调用 、全局和局部变量、异常捕获、事件
一:循环 for for in <!DOCTYPE html> <html> <head><meta charset="utf-8" / ...
- SpringBoot配置全局异常捕获
SpringBoot中自带的异常捕获机制返回的默认页面比较丑,对用户来说不够人性化.所以这篇文章来讲解SpringBoot钟自定义全局异常捕获. 本文的源码已经上传GitHub:https://git ...
- C#(二):数据类型、运算符、语句、类型转换、异常捕获、函数方法定义
数据类型.运算符.语句.类型转换.异常捕获.函数方法定义 命名约定 变量 文本 数字 布尔值 任意类型 `object` `dynamic` 声明局部变量 获取类型的默认值 数组 数组定义方法 `x ...
最新文章
- shell 监控局域网的主机是否up(转)
- [Ubuntu] 启动gvim时,怎样设置一个项目的文件为打开状态
- iOS开发之APP内部切换语言
- 解决关于登录校园网显示不在IP段的问题方案(要看注意事项哦!)
- java连接imserver_java后端IM消息推送服务开发——协议
- BZOJ 1878: [SDOI2009]HH的项链( BIT )
- 饭圈出征?《流浪地球》影迷给豆瓣App打一星:来啊互相伤害
- 2021年怎么自学前端?
- LeetCode 101 对称二叉树的几种思路(Python实现)
- termux无法安装引导程序包_Windows 10出现升级BUG:无法保留用户个人数据
- IEC 60335-1家用电器的安全标准及安规寿命检测设备
- 计算机地图制图的点状符号制作,点状符号
- 张勋说:溢流型棒磨机在水煤浆气化中的应用及技改(图文)
- Excel如何查找两列数据不同项
- 地理工具 | EXCEL读取照片EXIF,并在地图上标注拍摄地点
- 第十六届中国酒店“金枕头”奖获奖名单揭晓
- vscode如何打开html
- Python调用阿里API进行车牌识别
- linux 编辑文件 cat 跳到指定行,Linux基础命令(二)
- 通信原理包络是什么意思_2021年通信原理考研题库
热门文章
- netty系列之:channel,ServerChannel和netty中的实现
- Ansible篇-CentOS7安装AWX详解
- 浏览器空格键禁止滚动条滚动
- word 2013文字下面全是蓝色波浪线
- [转载]C罗皇马蜜月结束 “独性”不改或遭弃
- 微信小程序 连接云数据库(不使用云函数)进行 登录、注册、查询(包括模糊查询)快速实现 亲测可用
- nginx 稳定版 1.16.0 发布 支持动态加载SSL证书
- 捷联惯导-坐标系-观测值补偿-对准-编排-时间更新-测量更新
- Drupal 7:怎样去掉首页默认的“欢迎光临...”语句
- zabbix配置--拓扑图及链路流量