1 Star 0 Fork 0

c01dface/s-go-example

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
recover.go 1.06 KB
一键复制 编辑 原始数据 按行查看 历史
c01dface 提交于 2024-12-11 19:24 . add recover.go
package main
// Go makes it possible to recover from a panic, by using the
// recover built-in function. A recover can stop a panic from
// aborting the program and let it continue with execution instead.
// An example of where this can be useful: a server wouldn't want to
// crash if one of the client connections exhibits a critical error.
// Instead, the server would want to close that connection and continue
// serving other clients. In fact, this is what Go's net/http does
// by default for HTTP servers.
import "fmt"
// This function panics.
func mayPanic() {
panic("a problem")
}
func main() {
// recover must be called within a deferred function. When the
// enclosing function panics, the defer will activate and a
// recover call within it will catch the panic.
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered. Error:\n", r)
}
}()
mayPanic()
// This code will not run, because mayPanic() panics.
// The execution of main stops at the point of the panic and
// resumes in the deferred closure.
fmt.Println("After mayPanic()")
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/vmalloc/s-go-example.git
git@gitee.com:vmalloc/s-go-example.git
vmalloc
s-go-example
s-go-example
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385