Seven. Error handling

Table of contents

1. Error handling

1. Error generally handles some relatively low-level errors, which will not cause program interruption or downtime.

2. Panic is generally called when a fatal error occurs, such as an array out of bounds, a null pointer, etc.,

2.1 Manually call panic

2.2 Array out of bounds causes panic

2. recover function


1. Error handling

Exception handling (error-error, panic-panic, recover-recovery)

1. Error generally handles some relatively low-level errors, which will not cause program interruption or downtime.

Simply use the two error functions of the standard library:

package main 
import ( 
"errors" 
"fmt" 
) 
func main(){ 
err1 := fmt.Errorf("%s", "this is a Errorf") 
fmt.Println("err1: ", err1) 
err2 := errors.New("this New error") 
fmt.Println("err2: ", err2) 
} 

Common usage of error

package main

import (

"errors"

"fmt"

)

// error 为nil说明正常,否则报错,信息从errors.New获取

func MyDiv(a, b int) (result int, err error){

err = nil

if b == 0 {

err = errors.New("分母不能为0")

}else{

result = a/b

}

return // 等价于 return result, err

}

func main(){

ret, err := MyDiv(2, 2)

if err == nil{

fmt.Println("ret = ", ret)

}else{

fmt.Println("表达式存在非法值, err: ", err)

}

ret, err = MyDiv(2, 0)

if err == nil{

fmt.Println("ret = ", ret)

}else{

fmt.Println("表达式存在非法值, err: ", err)

}

}

// 最终可以通过这个函数获取一个errorString结构,从而可以调用Error方法。

func New(text string) error{

return &errorString(text)

}

============================================================

2. Panic is generally called when a fatal error occurs, such as an array out of bounds, a null pointer, etc.,

Of course, we can also manually call the panic() function to trigger. C-like assert() assertion function

number.

2.1 Manually call panic

package main

import "fmt"

func testa(){

fmt.Println("aaaaaaaaaaaaaa")

}

func testb(){

fmt.Println("bbbbbbbbbbbbbb")

// 手动调用panic()会触发断言

panic("manual triggered assertions, the program breaks")

}

func testc(){

fmt.Println("ccccccccccccccc")

}

func main(){

testa()

testb()

testc()

}

The result is as follows:

2.2 Array out of bounds causes panic

package main

import "fmt"

func testa(){

fmt.Println("aaaaaaaaaaaaaa")

}

func testb(index int){

// 数组越界造成panic断言

var x [10]int

fmt.Println("x: ", x[index])

}

func testc(){

fmt.Println("ccccccccccccccc")

}

func main(){

testa()

testb(10) // 数组越界触发断言

testc()

}

The result is as follows:

if it is a null pointer

2. recover function

When a panic error occurs, the program will be interrupted, but sometimes we don't want the program to be interrupted,

We can use the recover function to capture this interrupt. But need to pay attention to:

recover() is only valid for the function called by defer.

When defer is defined in the function and a panic error occurs in the function, the error will be caught

, the program will return to normal.

package main 
import "fmt" 
func testa(){ 
	fmt.Println("aaaaaaaaaaaaaa") 
} 
func testb(index int){ 
// 设置recover 
defer func () { 
	if err := recover() ; err != nil { 
	//fmt.Println("errInfo: ", recover())// 不要再次调用recover()作 
	为信息,因为此时调用是正常的, 
	//因为没有错误了,错误已经在if中被捕获掉 
	fmt.Println("errInfo: ", err) 
	} 
}() 
	// 数组越界造成panic断言 
	var x [10]int 
	fmt.Println("x: ", x[index]) 
} 
func testc(){ 
	fmt.Println("ccccccccccccccc") 
} 
func main(){ 
	testa() 
	testb(10) // 数组越界触发断言 
	testc() 
} 

The result is as follows:

Guess you like

Origin blog.csdn.net/m2282475145/article/details/131199786