Skip to content

Instantly share code, notes, and snippets.

@arindamroynitw
Created September 15, 2019 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arindamroynitw/199c8a9f4052e63eec68dfda71430abe to your computer and use it in GitHub Desktop.
Save arindamroynitw/199c8a9f4052e63eec68dfda71430abe to your computer and use it in GitHub Desktop.
Panic Recovery In Golang
package main
import "fmt"
func main() {
x := 20
y := 0
printAllOperations(x, y)
fmt.Println("Exiting main without any issues")
}
func printAllOperations(x int, y int) {
defer func() {
if r := recover(); r != nil {
fmt.Printf("Recovering from panic in printAllOperations error is: %v \n", r)
fmt.Println("Proceeding to alternative flow skipping division.")
printOperationsSkipDivide(x, y)
}
}()
sum, subtract, multiply, divide := x+y, x-y, x*y, x/y
fmt.Printf("sum=%v, subtract=%v, multiply=%v, divide=%v \n", sum, subtract, multiply, divide)
}
func printOperationsSkipDivide(x int, y int) {
sum, subtract, multiply := x+y, x-y, x*y
fmt.Printf("sum=%v, subtract=%v, multiply=%v \n", sum, subtract, multiply)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment