Skip to content

Commit 269211f

Browse files
committed
add un catch throw up
1 parent a067051 commit 269211f

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
当然这不是一个好的最佳实践,只是我们需要让业务能够正常跑起来更健壮以免隔三差五背锅,仅此而已!
88

9-
最后,切记:错误应该被尽早的暴露出来,而不是一味的掩盖!
9+
最后,切记:错误应该被尽早的暴露出来,而不是一味的掩盖![]()
1010

1111
# 二、安装
1212

@@ -154,6 +154,15 @@ func Test(t *testing.T) {
154154
fmt.Println("finally")
155155
}).Do()
156156

157+
// 发生panic,尝试捕获错误,但是没有捕获得到,则异常会被向上抛出,即仍然会panic
158+
try_catch.Try(func() {
159+
panic(errors.New("test"))
160+
}).Catch(errFoo, func(err error) {
161+
fmt.Println("catch success")
162+
}).Finally(func() {
163+
fmt.Println("not catch finally")
164+
}).Do()
165+
157166
}
158167
```
159168

example/example_chain_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,13 @@ func Test(t *testing.T) {
6464
fmt.Println("finally")
6565
}).Do()
6666

67+
// 发生panic,尝试捕获错误,但是没有捕获得到,则异常会被向上抛出,即仍然会panic
68+
try_catch.Try(func() {
69+
panic(errors.New("test"))
70+
}).Catch(errFoo, func(err error) {
71+
fmt.Println("catch success")
72+
}).Finally(func() {
73+
fmt.Println("not catch finally")
74+
}).Do()
75+
6776
}

try_catch_chain.go

+7
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,15 @@ func (x *TryHandler) Do() {
7878

7979
// 如果前面的捕获不到则走这里的Default,没设置的话就是不需要捕获了
8080
if !catchSuccess && x.bindingDefaultCatchHandler != nil {
81+
catchSuccess = true
8182
x.bindingDefaultCatchHandler.handle(err)
8283
}
84+
85+
// 如果错误没有被捕获到的话,则继续向上抛出
86+
if !catchSuccess {
87+
panic(err)
88+
}
89+
8390
}
8491

8592
// ------------------------------------------------ CatchHandler -------------------------------------------------------

try_catch_chain_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,13 @@ func Test(t *testing.T) {
6363
fmt.Println("finally")
6464
}).Do()
6565

66+
// 发生panic,尝试捕获错误,但是没有捕获得到,则异常会被向上抛出,即仍然会panic
67+
Try(func() {
68+
panic(errors.New("test"))
69+
}).Catch(errFoo, func(err error) {
70+
fmt.Println("catch success")
71+
}).Finally(func() {
72+
fmt.Println("not catch finally")
73+
}).Do()
74+
6675
}

0 commit comments

Comments
 (0)