Created
July 18, 2015 20:00
-
-
Save meson10/d56eface6f87c664d07d to your computer and use it in GitHub Desktop.
If you observe, In case of panic recovery you can only prevent the function from crashing at exit.
This file contains 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" | |
func main() { | |
f() | |
fmt.Println("Returned normally from f.") | |
} | |
func f() { | |
defer func() { | |
if r := recover(); r != nil { | |
fmt.Println("Recovered in f", r) | |
} | |
}() | |
fmt.Println("Calling g.") | |
g(0) | |
g(2) | |
g(3) | |
g(1) | |
fmt.Println("Returned normally from g.") | |
} | |
func g(i int) { | |
if i == 3 { | |
fmt.Println("Panicking!") | |
panic(fmt.Sprintf("%v", i)) | |
} else { | |
fmt.Println("Printing in g", i) | |
} | |
defer fmt.Println("Defer in g", i) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: