Solution Review: Generate Panic from Division by 0
This lesson discusses the solution to the challenge given in the previous lesson.
package mainimport ("fmt")func badCall() {a, b := 10, 0n := a / b // it will cause panic as it's a division by 0fmt.Println(n)}func test() {defer func() {if e := recover(); e != nil {fmt.Printf("Panicking %s\r\n", e);}}()badCall()fmt.Printf("After bad call\r\n"); // It won't be called, because we just recovered from an error}func main() {fmt.Printf("Calling test\r\n");test() // calling test functionfmt.Printf("Test completed\r\n");}
Get hands-on with 1200+ tech skills courses.