GO --- 02 language exception handling returns an error

Package main 

Import ( 
	"errors" 
	"FMT" 
	"Math" 
) 

/ * calculate the area of a circle, and returns Results - errors] * / 
FUNC GetCircleAreaII (RADIUS float64) (float64, error) { 
	IF RADIUS <0 { 
		ERR: = errors.New ( "Shaniao radius can not be negative it!") 
		return 0, ERR 
	} 
	return Math.PI * * rADIUS rADIUS, nil 
} 

FUNC main031 () { 
	Area, ERR: = GetCircleAreaII (-5) 
	! IF ERR = {nil 
		fmt.Println ( "to calculate an area, ERR =", ERR) 
	} the else { 
		fmt.Println ( "success calculate area, area =", area) 
	} 

	fmt.Println ( "other good things in life promised") 
	fmt.Println ( "GAME OVER") 
} 


/ * There is no need to return a result of the function can return an error object * / when performing error 
var asciichar rune = '0'

changeAsciiChar FUNC (newChar Rune) {error 
	IF newChar> {127 
		return errors.New ( "the incoming character is not an ASCII") 
	} 
	asciichar = newChar 
	return nil 
} 

FUNC main () { 
	ERR: = changeAsciiChar ( 'sister') 
	ERR IF! = nil { 
		fmt.Println ( "modified ASCII characters failure") 
	} the else { 
		fmt.Println ( "successfully modified") 
	} 
	fmt.Printf ( "C% \ n-", asciichar) 
}

  

Guess you like

Origin www.cnblogs.com/yunweiqiang/p/12065761.html