Created
September 15, 2019 09:43
-
-
Save arindamroynitw/199c8a9f4052e63eec68dfda71430abe to your computer and use it in GitHub Desktop.
Panic Recovery In Golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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