2016-02-17 02:14:05 +08:00
|
|
|
package util
|
2013-11-16 19:34:13 +08:00
|
|
|
|
2016-01-23 06:31:20 +08:00
|
|
|
// type marker for exceptions
|
2016-02-20 07:48:13 +08:00
|
|
|
type Exception struct {
|
|
|
|
Error error
|
2013-11-16 19:34:13 +08:00
|
|
|
}
|
|
|
|
|
2015-02-09 19:11:47 +08:00
|
|
|
// Throw panics with err wrapped properly so that it can be catched by Catch.
|
|
|
|
func Throw(err error) {
|
2016-02-20 07:48:13 +08:00
|
|
|
panic(Exception{err})
|
2013-11-16 19:34:13 +08:00
|
|
|
}
|
|
|
|
|
2015-02-09 19:11:47 +08:00
|
|
|
// Catch tries to catch an error thrown by Throw and stop the panic. If the
|
|
|
|
// panic is not caused by Throw, the panic is not stopped.
|
|
|
|
func Catch(perr *error) {
|
2013-11-16 19:34:13 +08:00
|
|
|
r := recover()
|
|
|
|
if r == nil {
|
|
|
|
return
|
|
|
|
}
|
2016-02-20 07:48:13 +08:00
|
|
|
if exc, ok := r.(Exception); ok {
|
|
|
|
*perr = exc.Error
|
2013-11-16 19:34:13 +08:00
|
|
|
} else {
|
|
|
|
panic(r)
|
|
|
|
}
|
|
|
|
}
|