Skip to content

Instantly share code, notes, and snippets.

@icexin

icexin/try.go Secret

Created May 5, 2022 15:26
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 icexin/fd39098dbd8e411d035573d893a46c33 to your computer and use it in GitHub Desktop.
Save icexin/fd39098dbd8e411d035573d893a46c33 to your computer and use it in GitHub Desktop.
Example of mock exception handling
package main
import (
"fmt"
"log"
)
func try(f func()) (err error) {
defer func() {
x := recover()
if x != nil {
err = fmt.Errorf("%v", x)
}
}()
f()
return
}
func panicme(msg string) {
panic(msg)
}
func main() {
err := try(func() {
panicme("some panic")
})
if err != nil {
log.Print("error:", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment