github.com/hoop33/elvish@v0.0.0-20160801152013-6d25485beab4/util/exception.go (about)

     1  package util
     2  
     3  // type marker for exceptions
     4  type Exception struct {
     5  	Error error
     6  }
     7  
     8  // Throw panics with err wrapped properly so that it can be catched by Catch.
     9  func Throw(err error) {
    10  	panic(Exception{err})
    11  }
    12  
    13  // Catch tries to catch an error thrown by Throw and stop the panic. If the
    14  // panic is not caused by Throw, the panic is not stopped.
    15  func Catch(perr *error) {
    16  	r := recover()
    17  	if r == nil {
    18  		return
    19  	}
    20  	if exc, ok := r.(Exception); ok {
    21  		*perr = exc.Error
    22  	} else {
    23  		panic(r)
    24  	}
    25  }