golang错误处理之panic和recover
panic()类似c++中的throw
recover()类似c++中的catchfunc CopyFile(SrcFileName, DstFileName string) (w int64, err error) {
defer func() {
if r := recover(); r != nil {
fmt.Printf("Runtime error is %v\n", r)
}
}()
SFile, e := os.Open(SrcFileName)
if e != nil {
panic("the src file is not exist...")
return
}
defer SFile.Close()
DFile, e := os.Create(DstFileName)
if e != nil {
panic("the dst file is not exist...")
return
}
defer DFile.Close()
// anonymous function
/*
defer func() {
SFile.Close()
DFile.Close()
}
*/
return io.Copy(DFile, SFile)
}