Created
May 26, 2021 14:24
-
-
Save wader/a3de44912c85e1ab37add5f7fda58791 to your computer and use it in GitHub Desktop.
go recover runtime error test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "runtime" | |
| ) | |
| func a(f func()) { | |
| defer func() { | |
| if rv := recover(); rv != nil { | |
| fmt.Println("recovered", rv) | |
| if _, ok := rv.(runtime.Error); ok { | |
| fmt.Println("runtime error") | |
| } | |
| } | |
| }() | |
| f() | |
| } | |
| func main() { | |
| fmt.Println("Hello, playground") | |
| a(func() { | |
| var a []int | |
| a[123] = 1 | |
| }) | |
| a(func() { | |
| panic("asdasd") | |
| }) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment