并发编程中错误处理有两种方法:主动错误处理(传播错误)和被动错误处理(错误管道)。恢复策略有两种:恢复(捕获恐慌)和重试(多次执行)。实战案例展示了这四种方法的使用。
在Go并发编程中,错误处理至关重要。有两种主要的方法来处理错误:
除了错误处理,并发编程中还存在恢复策略。恢复策略是当一个函数因错误而无法正常执行时采取的措施。有两种常见的恢复策略:
recover
函数捕获恐慌并恢复执行。func CalculateAverage(numbers []int) (float64, error) { if len(numbers) == 0 { return 0, errors.New("empty slice") } sum := 0 for _, number := range numbers { sum += number } return float64(sum) / float64(len(numbers)), nil } func main() { numbers := []int{1, 2, 3, 4, 5} average, err := CalculateAverage(numbers) if err != nil { fmt.Println(err) return } fmt.Println(average) }
type ErrorChannel chan error func CalculateAverageWithChannel(numbers []int) ErrorChannel { ch := make(ErrorChannel) go func() { if len(numbers) == 0 { ch <- errors.New("empty slice") return } sum := 0 for _, number := range numbers { sum += number } ch <- nil close(ch) }() return ch } func main() { numbers := []int{1, 2, 3, 4, 5} ch := CalculateAverageWithChannel(numbers) for err := range ch { if err != nil { fmt.Println(err) return } fmt.Println("Average calculated successfully") } }
func CalculateAverageWithRecovery(numbers []int) float64 { defer func() { if r := recover(); r != nil { fmt.Println("Error occurred:", r) } }() if len(numbers) == 0 { panic("empty slice") } sum := 0 for _, number := range numbers { sum += number } return float64(sum) / float64(len(numbers)) } func main() { numbers := []int{1, 2, 3, 4, 5} average := CalculateAverageWithRecovery(numbers) fmt.Println(average) }
func CalculateAverageWithRetry(numbers []int) (float64, error) { var err error = errors.New("empty slice") maxRetries := 3 for i := 0; i < maxRetries; i++ { if len(numbers) == 0 { err = errors.New("empty slice") continue } sum := 0 for _, number := range numbers { sum += number } return float64(sum) / float64(len(numbers)), nil } return 0, err } func main() { numbers := []int{1, 2, 3, 4, 5} average, err := CalculateAverageWithRetry(numbers) if err != nil { fmt.Println(err) } else { fmt.Println(average) } }
以上是Go并发编程中的错误处理与恢复策略的详细内容。更多信息请关注PHP中文网其他相关文章!