Go Error Handling
def feed(kid, food):
if food.calorie > 1000:
raise ValueError("this will kill your kid")
kid.eat(food)
def main():
# Initialize the objects...
try:
feed(eric, french_fries)
except ValueError:
print "please don't do that, he's fat, not big bone"func feed(k *Kid, f *Food) error {
if food.calorie > 1000 {
return errors.New("this will kill your kid")
}
k.eat(f)
return nil
}
func main() {
// Initialize the objects...
if err := feed(eric, frenchFries); err != nil {
fmt.Println("please don't do that, he's fat, not big bone")
log.Error(err) // Log the error
}
}Last updated