gotry

package module
v0.0.0-...-76e4fe5 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2019 License: MIT Imports: 0 Imported by: 0

README

golang + Try/Catch/Finally = GoTry

goTry (Golang for Try/Catch/Finally) is a Golang implementation for Exception handling. You can see an extended doc in godocs.

Try/Catch/Finally

This method is to Exception handling, in this case, when occurs a panic(), we can run a function instead that panic, maybe to solve that.
But is not recommendable to use this library, because golang have an explicit error handling.
See more info here.

goTry

  • First, You should download my library:

    go get github.com/fmorenovr/gotry/
    
  • Then, you should use for differents implements in Go.

    gotry.Try(func() {
      gotry.Throw("New exception")
    }).Catch(func(e gotry.Exception) {
      // Print crash
      fmt.Println("Catch ---> exception catched #1:", e)
    }).Finally(func() {
      fmt.Println("Finally ---> This always print after all try block #1")
    })
    
  • Could be a nested Try/Catch block

    gotry.Try(func() {
      gotry.Throw("New exception")
    }).Catch(func(e gotry.Exception) {
      // Print crash
      fmt.Println("Catch ---> exception catched #1:", e)
      gotry.Try(func(){
        gotry.Throw("New exception here!!")
      }).Catch(func(e gotry.Exception) {
        // Print crash
        fmt.Println("Catch ---> exception catched #2:", e)
      }).Finally(func() {
        fmt.Println("Finally ---> This always print after all try block #2")
      })
    }).Finally(func() {
      fmt.Println("Finally ---> This always print after all try block #1")
    })
    

Documentation

Overview

GoTry package is a Try/Catch/Finally library implemented in golang.

read more in Readme.md file

Example (NestedTryBlock)
package main

import (
	"fmt"
	"github.com/jenazads/gotry"
)

func main() {
	var obj interface{}
	obj = 2
	gotry.Try(func() {
		text := obj.(string)
		fmt.Println("Try ---> ", text)
	}).Catch(func(e gotry.Exception) {
		// Print crash
		fmt.Println("Catch ---> exception catched #1:", e)
		gotry.Try(func() {
			gotry.Throw("New exception here")
		}).Catch(func(e gotry.Exception) {
			// Print crash
			fmt.Println("Catch ---> exception catched #2:", e)
		}).Finally(func() {
			fmt.Println("Finally ---> This always print after all try block #2")
		})
	}).Finally(func() {
		fmt.Println("Finally ---> This always print after all try block #1")
	})
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Throw

func Throw(e Exception)

Throw function (return or rethrow an exception)

Types

type Exception

type Exception interface{}

Exception interface

type GoTry

type GoTry struct {
	Error Exception
	// contains filtered or unexported fields
}

Gotry object

func Try

func Try(funcToTry func()) (o *GoTry)

try this function

func (*GoTry) Catch

func (o *GoTry) Catch(funcCatched func(err Exception)) *GoTry

catch function

func (*GoTry) Finally

func (o *GoTry) Finally(finallyFunc func())

finally function

Jump to

Keyboard shortcuts

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