Created
August 28, 2016 14:51
-
-
Save hiraq/f103520e03306a232e590e81f969ba18 to your computer and use it in GitHub Desktop.
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" | |
) | |
type Callback func() | |
func JustPrintIt(msg string) { | |
fmt.Println(msg) | |
} | |
func catchThePanic(next ...Callback) { | |
if r := recover(); r != nil { | |
JustPrintIt(fmt.Sprintf("Recovered : %v", r)) | |
if len(next) > 0 { | |
for _, callback := range next { | |
callback() | |
} | |
} | |
} | |
} | |
func NextCall() { | |
JustPrintIt("Normal function") | |
} | |
func NextPanic() { | |
defer catchThePanic() | |
panic("second panic") | |
} | |
func DoPanic() { | |
defer catchThePanic(NextCall, NextPanic, NextCall) | |
defer JustPrintIt("O") | |
defer JustPrintIt("F") | |
defer JustPrintIt("I") | |
defer JustPrintIt("L") | |
panic("this is panic") | |
} | |
func main() { | |
DoPanic() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment