面向对象的编程语言往往都会支持语言级别的异常处理,比如c++使用关键字try开始异常语句块, catch捕获异常,throw抛出异常:
try {
throw 1;
throw std::logic_error("user defined exception");
} catch (int e) {
std::clog << e << std::endl;
} catch (const std::exception& e) {
std::clog << () << std::endl;
} catch (...) {
std::clog << "finally" << std::endl;
}
而golang的异常机制是:用panic抛出异常,然后在defer中调用recover()捕获异常。
怎么样在golang中实现类似try-catch的异常捕获机制呢?
这里提供一种简单的实现,使用Try函数开启异常捕获,Catch注册异常处理函数:
package try
import "reflect"
// Try catches exception from f
func Try(f func()) *tryStruct {
return &tryStruct{
catches: make(map[reflect.Type]ExeceptionHandler),
hold: f,
}
}
// ExeceptionHandler handle exception
type ExeceptionHandler func(interface{})
type tryStruct struct {
catches map[reflect.Type]ExeceptionHandler
hold func()
}
func (t *tryStruct) Catch(e interface{}, f ExeceptionHandler) *tryStruct {
t.catches[reflect.TypeOf(e)] = f
return t
}
func (t *tryStruct) Finally(f func()) {
defer func() {
if e := recover(); nil != e {
if h, ok := t.catches[reflect.TypeOf(e)]; ok {
h(e)
}
f()
}
}()
t.hold()
}
每次Catch注册异常处理函数需要传入一个具体值 + ExceptionHandler
,即可捕获具体值对应类型的异常。
调用代码如下:
import (
"log"
"try"
)
func main() {
try.Try(func() {
panic("123")
}).Catch(1, func(e interface{}) {
log.Println("int", e)
}).Catch("", func(e interface{}) {
log.Println("string", e)
}).Finally(func() {
log.Println("finally")
})
}