-
Notifications
You must be signed in to change notification settings - Fork 0
/
gotry.go
64 lines (59 loc) · 1.14 KB
/
gotry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package gotry
// Gotry object
type GoTry struct {
catch func(Exception)
finally func()
Error Exception
}
// Throw function (return or rethrow an exception)
func Throw(e Exception) {
if e == nil {
panic(gotry_rethrow)
} else {
panic(e)
}
}
// try this function
func Try(funcToTry func()) (o *GoTry) {
o = &GoTry{nil, nil, nil}
// catch throw in try
defer func() {
o.Error = recover()
}()
// do the func
funcToTry()
return
}
// catch function
func (o *GoTry) Catch(funcCatched func(err Exception)) (*GoTry){
o.catch = funcCatched
if o.Error != nil {
defer func() {
// call finally
if o.finally != nil {
o.finally()
}
// rethrow Gotrys from catch
if err := recover(); err != nil {
if err == gotry_rethrow {
err = o.Error
}
panic(err)
}
}()
o.catch(o.Error)
} else if o.finally != nil {
o.finally()
}
return o
}
// finally function
func (o *GoTry) Finally(finallyFunc func()) () {
if o.finally != nil {
panic("Finally Function by default !!")
} else {
o.finally = finallyFunc
}
defer o.finally()
return
}