golang错误处理之panic和recover
2015-12-13 21:46
555 查看
panic()类似c++中的throw
recover()类似c++中的catch
recover()类似c++中的catch
func 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) }
相关文章推荐
- 微博推荐算法学习(Weibo Recommend Algolrithm)
- golang声明变量
- HDU 1056 HangOver 模拟 水题
- Google Mock 启蒙篇
- MogoDB安装与使用(Windows篇)
- ReplaceGoogleCDN:将 Google CDN 更换国家
- [Evolutionary Algorithm] 进化算法简介
- How to import the www.googleapis.com SSL CA certification to the jks store file?
- 谁分配、谁释放的原则需要goto
- 面对世界竞争对手,如何拿到Google PDF开源项目PDFium?
- 类的扩展 :子类 类目 Category
- 最新 Google V8 编译 (2015-12-09)
- 无法从 ajax.googleapis.com 下载问题
- go slice
- ros由indigo版本升级至jade版本的烦恼
- django url 软编码
- Golang开发环境搭建-Vim篇
- zzulioj--1813--good string(模拟)
- zzulioj--1813--good string(模拟)
- Breakpad Google的crash捕获、抓取开源库