Skip to content

Instantly share code, notes, and snippets.

@StefansArya
Created October 8, 2019 02:17
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 StefansArya/0796ea05324c25dc66258162e055fd96 to your computer and use it in GitHub Desktop.
Save StefansArya/0796ea05324c25dc66258162e055fd96 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
)
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()
}
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")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment