try

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 12, 2024 License: MIT Imports: 3 Imported by: 1

README

go-try

test Go Report Card codecov Version Badge License Badge Go Reference

The try...catch statement alternative in Golang.

Installation

go get -u github.com/ghosind/go-try

Getting Started

There is the simplest example to run a function and handle the error in the catch function that the try function returned.

out, err := try.TryCatch(func () (error) {
  // Do something
  return err
}, func (err error) {
  // The function will not executing if err is nil.
  // Handle error...
})

The try functions will return two values. The first value is the all values the try function returned, and the second value is the error that the try function returns or it panics.

out, err := try.Try(func () (string, error) {
  return "Hello world", errors.New("expected error")
})
fmt.Println(out)
fmt.Println(err)
// [Hello world, expected error]
// expected error

The package provides four forms for the try statement alternative:

  • Try
  • TryFinally
  • TryCatch
  • TryCatchFinally

In default cases, the try functions will catch the error that the try function panics, and you can set the CatchPanic variable to false to disable it.

try.CatchPanic = false
try.Try(func () {
  panic("panic error")
})
// panic: expected error

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrNilFunction = errors.New("try function is required")
	ErrNotFunction = errors.New("try must be a function")
)
View Source
var CatchPanic bool = true

CatchPanic indicates whether the functions will catch the error that the try function panics or not.

Functions

func Try

func Try(try any) ([]any, error)

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

func TryCatch(try any, catch func(error)) ([]any, error)

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

func TryCatchFinally(try any, catch func(error), finally func()) ([]any, error)

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

func TryFinally(try any, finally func()) ([]any, error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL