2015-02-09 19:28:36 +08:00
|
|
|
package errutil
|
2013-11-16 19:34:13 +08:00
|
|
|
|
|
|
|
// An exception wraps an error.
|
|
|
|
type exception struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
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) {
|
2013-11-16 19:34:13 +08:00
|
|
|
panic(exception{err})
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
if exc, ok := r.(exception); ok {
|
|
|
|
*perr = exc.err
|
|
|
|
} else {
|
|
|
|
panic(r)
|
|
|
|
}
|
|
|
|
}
|