tryfinallycatch

master
Klaus Wendel 6 months ago
parent 8296d7dd26
commit 0041a4eecd

@ -0,0 +1,56 @@
/*
* MANY Thanks to https://dzone.com/articles/try-and-catch-in-golang and Marko Seidel for discovering it!
*/
package ebktools
// #############################################################################
// ### try, catch, finally - interface
// #############################################################################
type Block struct {
Try func()
Catch func(Exception)
Finally func()
}
type Exception interface{}
func Throw(up Exception) {
panic(up)
}
func (tcf Block) Do() {
if tcf.Finally != nil {
defer tcf.Finally()
}
if tcf.Catch != nil {
defer func() {
if r := recover(); r != nil {
tcf.Catch(r)
}
}()
}
tcf.Try()
}
/*
//Example:
func main() {
fmt.Println("We started")
Block{
Try: func() {
fmt.Println("I tried")
Throw("Oh,...sh...")
},
Catch: func(e Exception) {
fmt.Printf("Caught %v\n", e)
},
Finally: func() {
fmt.Println("Finally...")
},
}.Do()
fmt.Println("We went on")
}
*/
Loading…
Cancel
Save