Skip to content

Commit

Permalink
翻译13.3
Browse files Browse the repository at this point in the history
  • Loading branch information
dake committed Nov 7, 2015
1 parent 234e474 commit 839c803
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 3 deletions.
2 changes: 1 addition & 1 deletion eBook/13.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@ if err != nil {

- [目录](directory.md)
- 上一节:[错误处理](13.1.md)
- 下一节:[从错误中恢复 (Recover)](13.3.md)
- 下一节:[从 panic 中恢复 (Recover)](13.3.md)
84 changes: 83 additions & 1 deletion eBook/13.3.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,84 @@
# 13.3 从错误中恢复 (Recover)
# 13.3 从 panic 中恢复 (Recover)

正如名字一样,这个(recover)内建函数被用于从 panic 或 错误场景中恢复:让程序可以从 panicking 重新获得控制权,停止终止过程进而恢复正常执行。

`recover` 只能在 defer 修饰的函数(参见 6.4 节)中使用:用于取得 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 节中的例子)。

log 包用那些方法(methods)定义了一个 Logger 接口类型,如果你想自定义日志系统的话可以参考(参见 [http://golang.org/pkg/log/#Logger](http://golang.org/pkg/log/#Logger))。

这是一个展示 panic,defer 和 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)
2 changes: 2 additions & 0 deletions eBook/13.4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# 自定义包中的错误处理和 panicking

3 changes: 2 additions & 1 deletion eBook/directory.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@
- 第13章:[错误处理与测试](13.0.md)
- 13.1 [错误处理](13.1.md)
- 13.2 [运行时异常和 panic](13.2.md)
- 13.3 [从错误中恢复 (Recover)](13.3.md)
- 13.3 [从 panic 中恢复 (Recover)](13.3.md)
- 13.4 [自定义包中的错误处理和 panicking](13.4.md)
- 第14章:goroutine 与 channel
- 第15章:网络、模版与网页应用

Expand Down

0 comments on commit 839c803

Please sign in to comment.