try

package
v1.12.2 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2025 License: MIT Imports: 0 Imported by: 5

README

Try-Finally-Catch

Usage

Quick usage

import "github.com/gflydev/core/try"

try.Perform(func() {
    calledTry()
}).Finally(func() {
    calledFinally()
}).Catch(func(e try.E) {
    log.Errorf("Catch error %v", e)
})

Documentation

Index

Constants

View Source
const RethrowPanic = "___throw_it___"

RethrowPanic Special re-throw panic

Variables

This section is empty.

Functions

func Throw

func Throw(e E)

Throw function (return or rethrow an exception) Parameters:

  • e: The error to be thrown. If nil, a default panic with RethrowPanic value is thrown.

Types

type E

type E interface{}

E Error type

type EF

type EF func(err E)

EF Error function type

type F

type F func()

F Function type

type It

type It struct {
	Error E
	// contains filtered or unexported fields
}

It structure

func Perform

func Perform(funcToTry F) (o *It)

Perform registers the main-logic function and executes it. Parameters:

  • funcToTry: The main logic to execute within the try block.

Returns:

  • *It: An instance of the It structure containing the result and error (if any).

func (*It) Catch

func (o *It) Catch(funcCaught EF) *It

Catch registers the error-handling function that is executed if an error occurs. Parameters:

  • funcCaught: The function to handle the error, which receives the error as a parameter.

Returns:

  • *It: The same instance of the It structure for chaining.

func (*It) Finally

func (o *It) Finally(finallyFunc F) *It

Finally registers the finally-logic function that is executed at the end of the try-catch block. Parameters:

  • finallyFunc: The function containing cleanup or finalization logic to be executed.

Returns:

  • *It: The same instance of the It structure for chaining.

Jump to

Keyboard shortcuts

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