-
-
Save icexin/fd39098dbd8e411d035573d893a46c33 to your computer and use it in GitHub Desktop.
Example of mock exception handling
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" | |
"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