Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/handsomestwei/go-exception
golang error异常处理。提供类似java的try...catch...finally和try...with...resource机制。
https://github.com/handsomestwei/go-exception
golang try-catch-finally
Last synced: 14 days ago
JSON representation
golang error异常处理。提供类似java的try...catch...finally和try...with...resource机制。
- Host: GitHub
- URL: https://github.com/handsomestwei/go-exception
- Owner: handsomestWei
- License: apache-2.0
- Created: 2020-07-18T04:07:17.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-10-13T02:34:04.000Z (about 4 years ago)
- Last Synced: 2024-12-19T06:07:22.208Z (15 days ago)
- Topics: golang, try-catch-finally
- Language: Go
- Homepage:
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-exception
golang error异常处理。提供类似java的`try...catch...finally`和`try...with...resource`机制。# Usage
`try...catch...finally`需要显式调用Throw方法抛出异常才能被外层捕获。
```
tr := exception.NewTrier()
tr.Try(func() {
// 函数体
n1, err := strconv.Atoi("123")
tr.Throw(err)
n2, err := strconv.Atoi("qwe")
tr.Throw(err)
res := n1 / n2
fmt.Println(res)
}).Catch(func(e Exception) {
// 捕获异常和异常处理
fmt.Println("exception:", e)
}).Finally(func() {
// 事后处理
fmt.Println("finally")
})
````try...with...resource`对象必须实现io.Closer接口,处理结束后实现io自动关闭。
```
f, _ := os.Open("./exception_test.go")
trs := exception.NewTryResource(f)
trs.Try(func() {
// 函数体
scanner = bufio.NewScanner(f)
for scanner.Scan() {
fmt.Println(scanner.Text())
break
}
}).Catch(func(e Exception) {
// 捕获异常和异常处理
fmt.Println("exception:", e)
})
// 处理结束后自动关闭io流
```