Go中的Error&Panic| 青训营笔记

22 阅读3分钟

这是我参与「第五届青训营 」伴学笔记创作活动的第 3 天

引入

在Go语言中,Error和Panic都表示“错误”,但它们之间的严重程度却有所不同。

Error举例

以下案例选自Go官方文档。

// In Go it's idiomatic to communicate errors via an
// explicit, separate return value. This contrasts with
// the exceptions used in languages like Java and Ruby and
// the overloaded single result / error value sometimes
// used in C. Go's approach makes it easy to see which
// functions return errors and to handle them using the
// same language constructs employed for any other,
// non-error tasks.

package main

import (
	"errors"
	"fmt"
)

// By convention, errors are the last return value and
// have type `error`, a built-in interface.
func f1(arg int) (int, error) {
	if arg == 42 {

		// `errors.New` constructs a basic `error` value
		// with the given error message.
		return -1, errors.New("can't work with 42")

	}

	// A `nil` value in the error position indicates that
	// there was no error.
	return arg + 3, nil
}

// It's possible to use custom types as `error`s by
// implementing the `Error()` method on them. Here's a
// variant on the example above that uses a custom type
// to explicitly represent an argument error.
type argError struct {
	arg  int
	prob string
}

func (e *argError) Error() string {
	return fmt.Sprintf("%d - %s", e.arg, e.prob)
}

func f2(arg int) (int, error) {
	if arg == 42 {

		// In this case we use `&argError` syntax to build
		// a new struct, supplying values for the two
		// fields `arg` and `prob`.
		return -1, &argError{arg, "can't work with it"}
	}
	return arg + 3, nil
}

func main() {

	// The two loops below test out each of our
	// error-returning functions. Note that the use of an
	// inline error check on the `if` line is a common
	// idiom in Go code.
	for _, i := range []int{7, 42} {
		if r, e := f1(i); e != nil {
			fmt.Println("f1 failed:", e)
		} else {
			fmt.Println("f1 worked:", r)
		}
	}
	for _, i := range []int{7, 42} {
		if r, e := f2(i); e != nil {
			fmt.Println("f2 failed:", e)
		} else {
			fmt.Println("f2 worked:", r)
		}
	}

	// If you want to programmatically use the data in
	// a custom error, you'll need to get the error as an
	// instance of the custom error type via type
	// assertion.
	_, e := f2(42)
	if ae, ok := e.(*argError); ok {
		fmt.Println(ae.arg)
		fmt.Println(ae.prob)
	}
}

在这段代码中,我们使用了error.New()函数创建了一个错误,类似于Java中的new Exception(),不过,它们的处理方法有所不同。

如果要将它们抛出去该怎么做呢?

在Java中,我们需要使用throw关键字将一个Expection对象丢给父级处理。而在Go语言中,则是使用返回值的形式告知外界产生了错误,外界可以接受这个错误进行处理,也可以使用_这一符号将其忽略,相比之下,Go的实现更加简洁。

Error举例

以下案例选自Go官方文档。

// A `panic` typically means something went unexpectedly
// wrong. Mostly we use it to fail fast on errors that
// shouldn't occur during normal operation, or that we
// aren't prepared to handle gracefully.

package main

import "os"

func main() {

	// We'll use panic throughout this site to check for
	// unexpected errors. This is the only program on the
	// site designed to panic.
	panic("a problem")

	// A common use of panic is to abort if a function
	// returns an error value that we don't know how to
	// (or want to) handle. Here's an example of
	// `panic`king if we get an unexpected error when creating a new file.
	_, err := os.Create("/tmp/file")
	if err != nil {
		panic(err)
	}
}

在本地开发环境下,有如下提示:

image.png

可以发现,Create()函数被提示为“不可达的”,Panic()让程序停了下来,它的严重程度明显高于Error

Go官方文档指出,建议使用Error去替代Panic,只有当不知道如何处理这个错误或者不想处理这个错误的时候,才去使用Panic

总结

  • panic 用于真正异常的情况
  • error的严重程度低于panic
  • 开发过程中应尽量使用error而不是panic

如有疏漏,还望海涵。