• Stars
    star
    212
  • Rank 185,019 (Top 4 %)
  • Language
    Go
  • Created about 10 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Try/catch/finally in Go. This is an experiment that tries to bring the exception behaviour of java/python/c++ to Golang using the same syntax.

GoDoc

Try/Catch/Finally in Go

Experiment in Golang that tries to bring the exception behaviour of Java/Python/C++ to Golang using the same syntax.

Experiment only!!

I as a Go developer, do not recomend to use this library in production or real world code. The Go language was designed to do not use expections, instead use the explicit error management suggested in Effective Go. A good programmer MUST write idiomatic code.

Instead you should use this project as a learning tool to understand the exceptions flow in a language like python/c++ and python.

It also shows that Go, even without explicit exceptions has the semantics needed to provide it exactly in the same way Java/C++ and Python does.

Approach

  1. We need Try, Catch and Finally methods.
  2. We need a Throw() method for rethrowing exceptions.
  3. It needs to be stateless so it could be nested and used across many threads.

API examples:

1. Simple panic() inside Try

Unfortunately we have to include a Finally before a Catch. I have tried to find a way to avoid it, but looks impossible. Anyway, the behaviour and order of call is exactly the same than Java or Python.

import (
	"fmt"
	"github.com/manucorporat/try"
)

func main() {
	try.This(func() {
		panic("my panic")

	}).Finally(func() {
		fmt.Println("this must be printed after the catch")

	}).Catch(func(e try.E) {
		// Print crash
		fmt.Println(e)
	})
}

2. Finally is optional

import (
	"fmt"
	"github.com/manucorporat/try"
)

func main() {
	var obj interface{}
	obj = 2
	try.This(func() {
		// this operation will panic because obj is an integer
		text := obj.(string)
		fmt.Println(text)

	}).Catch(func(e try.E) {
		// Print crash
		fmt.Println(e)
	})
}

3. Rethrowing

import (
	"fmt"
	"github.com/manucorporat/try"
)

func main() {
	try.This(func() {
		panic("my panic")

	}).Finally(func() {
		fmt.Println("this must be printed after the catch")

	}).Catch(func(_ try.E) {
		fmt.Println("exception catched") // print
		try.Throw()                      // rethrow current exception!!
	})
}

4. Nested

package main

import (
	"fmt"
	"github.com/manucorporat/try"
)

func main() {
	try.This(func() {
		try.This(func() {
			panic("my panic")

		}).Catch(func(e try.E) {
			fmt.Println("fixing stuff") // print
			try.Throw()                 // rethrow current exception!!
		})

	}).Catch(func(e try.E) {
		// print
		fmt.Println(e)
	})
	fmt.Println("hey")
}

prints

fixing stuff
my panic
hey

Full covered with unit tests

Different cases of try/catch/finally

This Go package has the same behaviour than the implementation of exceptions in Java, C++ and Python.

1. No crash at all

try {
	print "1"
} catch(err) {
	print "2"
	print err
} finally {
	print "3"
}
print "4"

prints

1
3
4

2. Throw in try

try {
	print "1"
	throw "exception 1"
} catch(err) {
	print "2"
	print err
} finally {
	print "3"
}
print "4"

prints

1
2
exception 1
3
4

3. Throw in try and catch

try {
	print "1"
	throw "exception 1"
} catch(err) {
	print "2"
	throw "exception 2"
} finally {
	print "3"
}
print "4"

prints

1
2
3
---> uncatched exception 2

4. Throw in try, catch and finally

try {
	print "1"
	throw "exception 1"
} catch(err) {
	print "2"
	throw "exception 2"
} finally {
	print "3"
	throw "exception 3"
}
print "4"

prints

1
2
3
---> uncatched exception 3

yes! "exception 2" was throwed but "overwritten" by "exception 3"

5. finally is optional

try {
	print "1"
	throw "exception 1"
} catch(err) {
	print "2"
	throw "exception 2"
}
print "4"

prints

1
2
---> uncatched exception 2

5. Rethrowing exceptions

try {
	print "1"
	throw "exception 1"
} catch() {
	print "2"
	throw
}
print "4"

prints

1
2
---> uncatched exception 1

More Repositories

1

sse

Server-Sent Events implementation in Go. Used by the Gin Framework.
Go
271
star
2

perf-apis-2

96
star
3

AWTextureFilter

Apply a gaussian blur filter to your textures in cocos2D
Objective-C
75
star
4

CCNotifications

Notifications system in OpenGl for cocos2d
Objective-C
56
star
5

qwik-conference-app

TypeScript
41
star
6

ElectricFieldSimulation

An experimental example of how to use OpenGL for physical simulations. All the simulation runs concurrently in the GPU using my own engine as the rendering layer. GPUElectric potential and field simulation using FORZE2D lib. Universidad de valladolid 2013.
C++
33
star
7

OBME

OBME: OBfuscated MEmory. Fast and easy to use tool in simple C++ to avoid memory cheating by scanning (searching). See igameguardian, "Cheat Engine"...
C
31
star
8

openviewhealth

Open View Health is a free app anyone can use to visualize and securely share their medical data. A modern DICOM visualizer powered by Sethealth.
TypeScript
25
star
9

stencil-flower

JavaScript
11
star
10

CCArray

It's a high performance array written in Objective-C
C
10
star
11

promptle

Promptle is a game where players are given the output image generated by one of this systems and they have to guess the prompt used to generate it.
TypeScript
10
star
12

linter-verilog

Atom linter for Verilog, using icarus verilog.
CoffeeScript
9
star
13

stats

Go
9
star
14

pics-qwik

TypeScript
8
star
15

AWSecureDistribution

[MAC OS X] Don't worry to send confidential builds to the testers, press etc. Your builds will have a anti leak system (info).
C
8
star
16

FORZE2D

Innovator cross platform 2D engine based in cocos2d-iphone.
C++
8
star
17

manu-pu-v1

Single Cycle Harvard CPU, verilog + assembler
Verilog
8
star
18

talk-stencil-build-time-approach

Stencil: a build-time approach to the web -- presentation for JSconfEU 2019
8
star
19

obme-ios

Objective-C
7
star
20

golru

GoLRU is a efficient LRU cache written in Golang that uses a approximated LRU eviction policy (the same used by redis)
Go
7
star
21

ranfe-unofficial

TypeScript
7
star
22

talk-stencil-then-now-future

6
star
23

asset

Reference asset files easier
Go
6
star
24

noise-lang

JavaScript
6
star
25

stencil-framework-to-compiler

5
star
26

stencil-one-runtime-demo

JavaScript
4
star
27

edge-functions-examples

JavaScript
4
star
28

qwik-playground

JavaScript
4
star
29

tonic

Go
3
star
30

simple-aes

Go
3
star
31

smartProfiler

Smart and easy way to profile algorithms in C.
C
3
star
32

vite-plugin-macro

Rust
3
star
33

vite-repo-project

CSS
3
star
34

cryptoutils

Go
2
star
35

easyPGP

Go
2
star
36

xor-strings

C
2
star
37

gopower

Go
1
star
38

qwik-app

TypeScript
1
star
39

perf-apis

1
star
40

cpu

Assembly
1
star
41

vermoji-commit

emojis for committing
TypeScript
1
star
42

ionic-core-starter

TypeScript
1
star