Comments (3)
不知能否具体举例说明,多谢了!
在这种条件语句中有可能执行不到defer:
func t(i int) int {
fmt.Println(i)
i++
if i > 0 {
return i
} else {
defer func() {
fmt.Println(i)
}()
return i
}
}
from go42.
因为自己踩过坑,所以贴出来,让大家注意一下,谢谢笔者总结。
例子:
`func TestDefer() {
fmt.Println("start")
panic("generate panic.")
defer func() { // defer是无法执行的,导致recover是无效的
ret := recover()
if ret != nil {
fmt.Println("TestDefer panic")
} else {
fmt.Println("TestDefer has no panic")
}
}()
}`
from go42.
多谢说明。这是因为在Go中规定:panic之前需要先声明好defer。
15.2 Panic
在Go语言中 panic 是一个内置函数,用来表示非常严重的不可恢复的错误。必须要先声明defer,否则不能捕获到panic异常。
from go42.
Related Issues (20)
- 15.2 Panic 的案例 ParseError 是什么? HOT 1
- Is there any gitbook version HOT 1
- 第19章接口最后代码 HOT 1
- Could you please provide pdf or epub format for this nice book? HOT 1
- string转[]byte的陷阱 HOT 1
- 42.7节拼写错误 HOT 1
- 字典 map 那章 “所以数组、函数、字典、切片和结构体不能作为 key”,其实数组可以作为 key 存在 HOT 1
- 第三十二章 fmt包与日志log包表格中多个冒号 HOT 1
- 这个地方是不是错了,应该互斥锁获得了不会连续打印两个locked HOT 3
- 第十章关于strings.Join和strings.Builder效率比较的问题
- 关于23.1 同步锁代码示例的错误 HOT 4
- 关于28.2 指针运算代码示例的错误 HOT 1
- 关于31.4 bufio包代码示例的错误 HOT 1
- 请问是否可以转载 HOT 1
- 23.1 同步锁示例中G1解锁后,G2和G3中一个加锁后另一个需要等待锁释放后才能加锁 HOT 1
- 42_08_project.md建议增加goproxy的说明 HOT 1
- 好名字
- 请问实体书和电子版的内容一模一样吗?
- 14章使用迭代参数,新版本的go已经修复了那个可能显示三个three的问题 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go42.