elvish/util/exception.go

26 lines
497 B
Go
Raw Normal View History

package util
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
}
// 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})
}
// 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) {
r := recover()
if r == nil {
return
}
2016-02-20 07:48:13 +08:00
if exc, ok := r.(Exception); ok {
*perr = exc.Error
} else {
panic(r)
}
}