标签:
type error interface{
Error() string
}
func Foo(param int)(n int,err error){
//函数定义
}
n,err:=Foo(0)
if err!=nil{
//错误处理
}else{
//使用返回值n
}
type PathError struct{
op string
path string
Err error
}
func (e *PathError) Error() string{
return e.op+" "+e.path+":"+e.Err.Error()
}
func CopyFile(dst, src string) {
defer fmt.Println(dst)
defer fmt.Println(src)
fmt.Println("copy")
}
copy
c:\2.txt
c:\oem8.txt
panic:相当于throw,recover:相当于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
这里panic后面的fmt.Println("b")未被执行,所以后面的信息没有b
标签:
原文地址:http://www.cnblogs.com/anbylau2130/p/4240851.html