Go错误处理

栏目: Go · 发布时间: 7年前

内容简介:错误处理前报错后程序退出

错误处理前

package main

import "fmt"

func test(){


   num1  := 10
   num2  := 0
   res := num1 / num2  //0不能做除数
   fmt.Println(res)
}
func  main(){
   //测试
   test()
   fmt.Println("执行结果")
}

Go错误处理

报错后程序退出

错误处理后

package main

import "fmt"

func test(){
   defer func() {
      err := recover()
      if  err != nil {
         //说明捕获到异常
         fmt.Print("err=",err)
      }
   }()

   num1  := 10
   num2  := 0
   res := num1 / num2
   fmt.Println(res)
}
func  main(){
   //测试
   test()
   fmt.Println("执行结果")
}

Go错误处理

进行错误处理后,程序不会轻易挂掉,如果加入预警代码,就可以让程序更加的健壮!


以上所述就是小编给大家介绍的《Go错误处理》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Beginning Google Maps API 3

Beginning Google Maps API 3

Gabriel Svennerberg / Apress / 2010-07-27 / $39.99

This book is about the next generation of the Google Maps API. It will provide the reader with the skills and knowledge necessary to incorporate Google Maps v3 on web pages in both desktop and mobile ......一起来看看 《Beginning Google Maps API 3》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具