Files
the-way-to-go_ZH_CN/eBook/13.3.md
2015-11-09 00:25:14 +08:00

85 lines
2.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 13.3 从 panic 中恢复Recover
正如名字一样这个recover内建函数被用于从 panic 或 错误场景中恢复:让程序可以从 panicking 重新获得控制权,停止终止过程进而恢复正常执行。
`recover` 只能在 defer 修饰的函数(参见 [6.4 节](06.4.md))中使用:用于取得 panic 调用中传递过来的错误值,如果是正常执行,调用 `recover` 会返回 nil且没有其它效果。
<u>总结</u>panic 会导致栈被展开直到 defer 修饰的 recover() 被调用或者程序中止。
下面例子中的 protect 函数调用函数参数 g 来保护调用者防止从 g 中抛出的运行时 panic并展示 panic 中的信息:
```go
func protect(g func()) {
defer func() {
log.Println(done)
// Println executes normally even if there is a panic
if err := recover(); err != nil {
log.Printf(run time panic: %v, err)
}
}()
log.Println(start)
g() // possible runtime-error
}
```
这跟 Java 和 .NET 这样的语言中的 catch 块类似。
log 包实现了简单的日志功能:默认的 log 对象向标准错误输出中写入并打印每条日志信息的日期和时间。除了 `Println``Printf` 函数,其它的致命性函数都会在写完日志信息后调用 os.Exit(1),那些退出函数也是如此。而 Panic 效果的函数会在写完日志信息后调用 panic可以在程序必须中止或发生了临界错误时使用它们就像当 web 服务器不能启动时那样(参见 [15.4 节](15.4.md)中的例子)。
log 包用那些方法methods定义了一个 Logger 接口类型,如果你想自定义日志系统的话可以参考(参见 [http://golang.org/pkg/log/#Logger](http://golang.org/pkg/log/#Logger))。
这是一个展示 panicdefer 和 recover 怎么结合使用的完整例子:
示例 13.3 [panic_recover.go](examples/chapter_13/panic_recover.go)
```go
// panic_recover.go
package main
import (
"fmt"
)
func badCall() {
panic("bad end")
}
func test() {
defer func() {
if e := recover(); e != nil {
fmt.Printf("Panicing %s\r\n", e)
}
}()
badCall()
fmt.Printf("After bad call\r\n") // <-- wordt niet bereikt
}
func main() {
fmt.Printf("Calling test\r\n")
test()
fmt.Printf("Test completed\r\n")
}
```
输出:
```
Calling test
Panicing bad end
Test completed
```
`defer-panic-recover` 在某种意义上也是一种像 `if``for` 这样的控制流机制。
Go 标准库中许多地方都用了这个机制例如json 包中的解码和 regexp 包中的 Complie 函数。Go 库的原则是即使在包的内部使用了 panic在它的对外接口API中也必须用 recover 处理成返回显式的错误。
## 链接
- [目录](directory.md)
- 上一节:[错运行时异常和 panic](13.2.md)
- 下一节:[自定义包中的错误处理和 panicking](13.4.md)