Skip to content

Commit

Permalink
add panicrecover
Browse files Browse the repository at this point in the history
  • Loading branch information
imjoseangel committed Aug 28, 2024
1 parent 75cf397 commit 828c501
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions go/deferpanicrecover/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import "fmt"

func c() (i int) {
defer func() { i++ }()
return 2
}

func f() {
deferfunc() {
if r := recover(); r != nil {
fmt.Println("Recovered in f", r)
}
}()
fmt.Println("Calling g.")
g(0)
fmt.Println("Returned normally from g.")
}

func g(i int) {
if i > 3 {
fmt.Println("Panicking!")
panic(fmt.Sprintf("%v", i))
}
defer fmt.Println("Defer in g", i)
fmt.Println("Printing in g", i)
g(i + 1)
}

func main() {
c := c()
fmt.Println(c)

f()
fmt.Println("Returned normally from f.")
}

0 comments on commit 828c501

Please sign in to comment.