Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrNilFunction = errors.New("try function is required") ErrNotFunction = errors.New("try must be a function") )
var CatchPanic bool = true
CatchPanic indicates whether the functions will catch the error that the try function panics or not.
Functions ¶
func Try ¶
Try executes the try function, and returns the error that the try function returned. It will also catch the panic error if it runs under CatchPanic mode.
Example ¶
out, err := try.Try(func() int { // TODO return 0 }) fmt.Println(out) fmt.Println(err)
Output: [0] <nil>
func TryCatch ¶
TryCatch executes the try function, and if it returns an error or panics under CatchPanic mode, the catch function will be executed.
Example ¶
out, err := try.TryCatch(func() error { return errors.New("expected error") }, func(err error) { fmt.Printf("error in catch: %v\n", err) }) fmt.Println(out) fmt.Println(err)
Output: error in catch: expected error [expected error] expected error
func TryCatchFinally ¶
TryCatchFinally executes the try function, and if it returns an error or panics under CatchPanic mode, the catch function will be executed. The finally function will always be executed except the catch function panics.
Example ¶
out, err := try.TryCatchFinally(func() error { return errors.New("expected error") }, func(err error) { fmt.Printf("error in catch: %v\n", err) }, func() { fmt.Println("in finally") }) fmt.Println(out) fmt.Println(err)
Output: error in catch: expected error in finally [expected error] expected error
func TryFinally ¶
TryFinally executes the try function, and it executes the finally function after the try function is finished.
Example ¶
out, err := try.TryFinally(func() error { return errors.New("expected error") }, func() { fmt.Println("in finally") }) fmt.Println(out) fmt.Println(err)
Output: in finally [expected error] expected error
Types ¶
This section is empty.