Last active
July 14, 2022 14:58
-
-
Save nwillc/019fc24269f4d5897a97e6e7dbbea7e2 to your computer and use it in GitHub Desktop.
Go showing basic panic recover
This file contains hidden or 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
func CaculateSomething(i int) (result int, err error) { | |
// Does a bunch of math to calculate a value, or | |
// returns an error when it can't. | |
defer func() { | |
if r := recover(); r != nil { | |
err = fmt.Errorf("panic: %+v", r) | |
} | |
}() | |
// ... the magic ... | |
return result, err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment