7-2恐慌处理

package main

import (
    "fmt"
    "math"
)

//求小球的体积
//如果半径是负数,返回pannic
//处理pannic,温和的返回结果

func GetToyBallvolume(redius  float64)float64  {

    if redius<0 {

        //redius 为负数,直接返回恐慌
        panic("半径不可以为负数")
    }
    return (4/3.0*math.Pi*math.Pow(redius,3))
}

func main() {

    defer func() {
        //recover 复活
        if err := recover();err != nil{
            fmt.Println("程序异常的原因是,",err)
        }

    }()

    ballvolume := GetToyBallvolume(-1)
    fmt.Println(ballvolume)
}

 

posted @ 2019-07-29 17:32  pad+  阅读(150)  评论(0编辑  收藏  举报