definition of the 1,.error interface
type error interface{
Error() string
}
Use of 2.error
func Foo(param int)(n int,err error){
//函数定义
}
n,err:=Foo(0)
if err!=nil{
//错误处理
}else{
//使用返回值n
}
3. Custom Error Types
- Defines a struct that hosts error messages
- < Code class= "Language-go" > type patherror struct {
op string
< Span class= "PLN" > path string
err Span class= "PLN" > error
}
- Defining the Error method
func (e *PathError) Error() string{
return e.op+" "+e.path+":"+e.Err.Error()
}
defer: Equivalent to the filnaly in C #, is always executed, and his execution order is advanced after
func CopyFile(dst, src string) {
defer fmt.Println(dst)
defer fmt.Println(src)
fmt.Println("copy")
}
Copy
C:\2.txt
C:\oem8.txt
Panic: equivalent to Throw,recover: equivalent to catch
defer func() { //必须要先声明defer,否则不能捕获到panic异常
if err := recover(); err != nil {
fmt.Println(err) //这里的err其实就是panic传入的内容,55
}
}()
f()
func f() {
fmt.Println("a")
panic(55)
fmt.Println("b")
}
A
55
Here panic the fmt behind. Println ("B") was not executed, so the information behind it was not B
From for notes (Wiz)
Go error handling (ii)