Created
September 12, 2016 04:06
-
-
Save bradclawsie/394e0c1ccf0d813e029b5125f65bca1a to your computer and use it in GitHub Desktop.
panic.go
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 ( | |
"errors" | |
"log" | |
) | |
func final() { | |
log.Printf("cleaning up") | |
if c := recover(); c != nil { | |
if err,ok := c.(error); ok { | |
// Do something useful...then: | |
log.Fatal(err.Error()) | |
} else { | |
log.Printf("unexpected panic") | |
panic(c) | |
} | |
} | |
} | |
func main() { | |
log.Printf("hello") | |
defer final() | |
log.Printf("goodbye") | |
panic(errors.New("oh dear!")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment