The golang implementation encapsulates the trycatch exception handling instance code


preface

Many people in the community are talking about golang instead of trycatch, adopting the harsh combination of recovery, panic and defer… I made a whole trycatch bag from the Internet, I feel good, take it out and share it… Without further ado, let’s start with a detailed introduction.

The sample code

package main

import (
 "reflect"
)

type TryCatch struct {
 errChan  chan interface{}
 catches  map[reflect.Type]func(err error)
 defaultCatch func(err error)
}

func (t TryCatch) Try(block func()) TryCatch {
 t.errChan = make(chan interface{})
 t.catches = map[reflect.Type]func(err error){}
 t.defaultCatch = func(err error) {}
 go func() {
 defer func() {
 t.errChan <- recover()
 }()
 block()
 }()
 return t
}

func (t TryCatch) CatchAll(block func(err error)) TryCatch {
 t.defaultCatch = block
 return t
}

func (t TryCatch) Catch(e error, block func(err error)) TryCatch {
 errorType := reflect.TypeOf(e)
 t.catches[errorType] = block
 return t
}

func (t TryCatch) Finally(block func()) TryCatch {
 err := <-t.errChan
 if err != nil {
 catch := t.catches[reflect.TypeOf(err)]
 if catch != nil {
 catch(err.(error))
 } else {
 t.defaultCatch(err.(error))
 }
 }
 block()
 return t
}

type MyError struct {
 error
}

func main() {
 TryCatch{}.Try(func() {
 println("do something buggy")
 panic(MyError{})
 }).Catch(MyError{}, func(err error) {
 println("catch MyError")
 }).CatchAll(func(err error) {
 println("catch error")
 }).Finally(func() {
 println("finally do something")
 })
 println("done")
}

conclusion