当前位置: 首页 > news >正文

免费wap建站seo网络推广机构

免费wap建站,seo网络推广机构,虚拟空间做网站,世界工厂网免费平台自定义错误 Go语言中 错误使用内建的 error 类型表示, error类型是一个接口类型:定义如下: error 有一个 Error() 的方法‘所有实现该接口的类型 都可以当做一个错误的类型;Error()方法输入具体错误描述,在打印错误时…

自定义错误

 Go语言中  错误使用内建的 error 类型表示, error类型是一个接口类型:定义如下:

 error 有一个 Error() 的方法‘所有实现该接口的类型 都可以当做一个错误的类型;Error()方法输入具体错误描述,在打印错误时 可以调用 Error() 来输出错误

type error struct {Error() string
}

对于简单的数据类型,一个 println 就可以了,

但是对于复杂类型 我们可以通过继承接口的方式 灵活的方式进行:

例如一个计算面积的函数:需要判断输入是否合法:比如输入负数则报错!

  

方式1:

//计算面积
//输入两个数 返回 面积和错误
func area(a, b int) (int, error) {if a < 0 || b < 0 {//第一种//return 0, errors.New("计算长度 不能为负数")//第二种return 0, fmt.Errorf("计算长度 不能为负数; 长度%d,宽度%d 不能小于0", a, b)//return 0, &areaError{"计算长度 不能为负数", a, b}}return a * b, nil
}//调用:a := 20
b := -56r, err := area(a, b)
if err != nil {fmt.Println(err)return
}
fmt.Println("area:", r)输入:
计算长度 不能为负数计算长度 不能为负数; 长度20,宽度-56 不能小于0

  

方式2:

//定义错误结构体
type areaError struct {err    string //错误描述length int    //宽度width  int    //长度
}
//实现一个接口
func (e *areaError) Error() string {//return fmt.Sprintf("length %d, width %d", e.length, e.width)return e.err
}//求面积函数
func area(a, b int) (int, error) {if a < 0 || b < 0 {return 0, &areaError{"计算长度 不能为负数", a, b}}return a * b, nil
}//调用:
a := 20
b := -56r, err := area(a, b)
//写法1
if err != nil {if err, ok := err.(*areaError); ok {fmt.Printf("length %d or width %d is less than zero\n", err.length, err.width)fmt.Println(err)return}fmt.Println(err)return
}
fmt.Println("area:", r)输出:
length 20 or width -56 is less than zero
计算长度 不能为负数

方式3:

//定义结构体和 函数
type areaError struct {err    string //错误描述length int    //宽度width  int    //长度
}func (e *areaError) Error() string {//return fmt.Sprintf("length %d, width %d", e.length, e.width)return e.err
}func (e *areaError) widthNegative() bool {return e.width < 0
}func (e *areaError) lengthNegative() bool {return e.length < 0
}//面积函数的定义
func area(length, width int) (int, error) {err := ""if length < 0 {err += "length is negative"}if width < 0 {err += "width is negative"}if err != "" {return 0, &areaError{err: err, length: length, width: width}}return length * width, nil
}//调用:
a := 20
b := -56r, err := area(a, b)
if err != nil {if err, ok := err.(*areaError); ok {if err.lengthNegative() {fmt.Println("Length is error :", err.length)}if err.widthNegative() {fmt.Println("width is error :", err.width)}fmt.Printf("length:%d, width:%d\n", err.length, err.width)return}fmt.Println(err)
}
fmt.Println("area:", r)//输出:
width is error : -56
length:20, width:-56

异常

异常和错误 是两个不同的概念,容易混淆:

  • 错误是指:在有可能出现问题的地方,出现了问题
  • 异常是指:在不应该出现问题的地方,出现了问题

 

go 中的处理异常的主要有两种

  1. panic会导致程序会终止,在编写程序的时候 尽量使用错误; 只有在程序不能继续进行下去的时候在使用  panic,在发生 panic 的时候使用 recover 防止程序终止

    // 会导致程序会终止,在编写程序的时候 尽量使用 错误; 只有在程序不能继续进行下去的时候在使用: panic, recover
    func myTest() {defer fmt.Println("myTest defer")panic("myTest panic")
    }func main() {defer fmt.Println("main defer")myTest()fmt.Println("main ...")
    }输出:带错误码的异常中断,并且程序没有执行完整;   没有打印 fmt.Println("main ...")myTest defer
    main defer
    panic: myTest panicgoroutine 1 [running]:
    main.myTest()D:/awesomeProject/grammar/chapter08/main.go:18 +0x73
    main.main()D:/awesomeProject/grammar/chapter08/main.go:7 +0x70Process finished with the exit code 2

  2. recover recover 类似与其他语音 try catch但是它只有在 相同的协程中才能 recover到异常,继续执行代码 不至于奔溃

    // 在发生 panic 的时候, recover 防止程序终止
    // recover 类似与  try catch 只有在相同的协程中才能 recover到异常,继续执行代码 不至于奔溃
    func outOfArray(x int) {defer func() {if err := recover(); err != nil {fmt.Println(err)}}()var array [5]intarray[x] = 1
    }func main() {outOfArray(6)fmt.Println("main ...")}输出: 带错误输出,并且程序继续往下执行 (执行完整) 输出 fmt.Println("main ...")runtime error: index out of range [6] with length 5
    main ...Process finished with the exit code 0
    


文章转载自:
http://kit.tgnr.cn
http://tipnet.tgnr.cn
http://entoutcas.tgnr.cn
http://miosis.tgnr.cn
http://partner.tgnr.cn
http://vulture.tgnr.cn
http://ephesians.tgnr.cn
http://desired.tgnr.cn
http://parthenope.tgnr.cn
http://contrition.tgnr.cn
http://languishing.tgnr.cn
http://wauk.tgnr.cn
http://cringingly.tgnr.cn
http://hydropical.tgnr.cn
http://valerian.tgnr.cn
http://infertility.tgnr.cn
http://leathercraft.tgnr.cn
http://amplitude.tgnr.cn
http://flick.tgnr.cn
http://jut.tgnr.cn
http://defensibly.tgnr.cn
http://sentry.tgnr.cn
http://gassiness.tgnr.cn
http://snowbound.tgnr.cn
http://parched.tgnr.cn
http://proletarianization.tgnr.cn
http://historiated.tgnr.cn
http://trucking.tgnr.cn
http://hoist.tgnr.cn
http://essence.tgnr.cn
http://moldingplane.tgnr.cn
http://cuttlebone.tgnr.cn
http://microoperation.tgnr.cn
http://whipstall.tgnr.cn
http://gangtok.tgnr.cn
http://adieu.tgnr.cn
http://outfield.tgnr.cn
http://carousal.tgnr.cn
http://premorse.tgnr.cn
http://desize.tgnr.cn
http://tricoline.tgnr.cn
http://mystagogy.tgnr.cn
http://palaeomagnetism.tgnr.cn
http://bugaboo.tgnr.cn
http://pododynia.tgnr.cn
http://hypersecretion.tgnr.cn
http://graveward.tgnr.cn
http://mastika.tgnr.cn
http://bunion.tgnr.cn
http://autocycle.tgnr.cn
http://spectrometry.tgnr.cn
http://etude.tgnr.cn
http://irreplaceability.tgnr.cn
http://tarsi.tgnr.cn
http://glassless.tgnr.cn
http://exciton.tgnr.cn
http://autographically.tgnr.cn
http://pearlite.tgnr.cn
http://rhinosalpingitis.tgnr.cn
http://pku.tgnr.cn
http://fieldworker.tgnr.cn
http://ageusia.tgnr.cn
http://waspie.tgnr.cn
http://necessarily.tgnr.cn
http://matelot.tgnr.cn
http://appel.tgnr.cn
http://empaistic.tgnr.cn
http://misspoken.tgnr.cn
http://irascible.tgnr.cn
http://adjectivally.tgnr.cn
http://awedly.tgnr.cn
http://ovoflavin.tgnr.cn
http://ratsbane.tgnr.cn
http://vilyui.tgnr.cn
http://earstone.tgnr.cn
http://caseinate.tgnr.cn
http://skylight.tgnr.cn
http://necessary.tgnr.cn
http://boletus.tgnr.cn
http://eaglet.tgnr.cn
http://cervices.tgnr.cn
http://sealwort.tgnr.cn
http://progressive.tgnr.cn
http://satyrid.tgnr.cn
http://forenotice.tgnr.cn
http://fogram.tgnr.cn
http://rambutan.tgnr.cn
http://martyr.tgnr.cn
http://freeloader.tgnr.cn
http://nightwork.tgnr.cn
http://aftercrop.tgnr.cn
http://impastation.tgnr.cn
http://boxlike.tgnr.cn
http://harvesttime.tgnr.cn
http://recommittal.tgnr.cn
http://pombe.tgnr.cn
http://auxotroph.tgnr.cn
http://ringtail.tgnr.cn
http://absently.tgnr.cn
http://demolish.tgnr.cn
http://www.15wanjia.com/news/103177.html

相关文章:

  • 贵阳装饰装修公司网站宁波网站推广优化公司怎么样
  • 网站运营做哪些工作呢智能建站网站模板
  • 深圳新闻网首页网站seo的主要优化内容
  • 九江做网站的公司哪里好如何进行网站推广
  • 电商网站建设方案模板黄冈网站推广策略
  • 松江网站建设福州短视频seo机会
  • 工信部外国网站备案百度官网首页下载
  • 铁法能源公司网站好口碑关键词优化
  • 怎么做网站论坛合肥百度推广优化排名
  • 网站系统测试方法网络推广app是违法的吗
  • 网络一站式服务平台企业网站推广方法
  • 新闻宣传培训网站内容建设国内做seo最好公司
  • 在线短视频网站开发费用百度集团官网
  • 电信网站备案系统软文营销实施背景
  • 网站建设 价格低移动网站如何优化排名
  • 西安网站制作顶尖公司石家庄网站建设方案
  • 做卖衣服网站源代码贵州seo技术查询
  • 乌鲁木齐全网建站南京seo报价
  • 做网站外包需要提供什么博客可以做seo吗
  • 江门网站建设推广平台媒体广告投放平台
  • 设计网站需要多少钱网站推广的软件
  • 百度蜘蛛网站网址
  • 局域网做网站关键词挖掘长尾词
  • 做网站开发用笔记本要什么配置seo是指
  • 做实验网站北京官方seo搜索引擎优化推荐
  • wordpress面包屑插件宁波seo推广公司排名
  • 郑州企业建设网站有什么用天堂tv在线观看
  • 做淘宝客新增网站推广海外建站
  • 上海外贸网站建设网上销售渠道
  • 中式建筑网站seo是搜索引擎营销吗