Skip to content

ftloc/exception

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 

Repository files navigation

GoDoc Build Status Coverage Status

exception

exception handling for golang

usage

basics

Basically Try/Catch are just some function wrappers that recover from panics.

exception.Try(func(){
	// something that might go wrong
    exception.Throw(fmt.Errorf("Some error"))
}).Catch(func(e error){
	log.Warningf("An error occured: %s", e)
}).Go()

multiple errors

The handler function is chosen by the function signature, that takes the the given object as first and only argument.

type Exception struct{
	Message string
}
exception.Try(func(){
	// something that might go wrong
    exception.Throw(Exception{Message:"This is an exception"})
}).Catch(func(e Exception){
	log.Warningf("An exception occured: %s", e.Message)
}).Catch(func(e error){
	log.Warningf("An error occured: %s", e)
}).Go()

unknown errors

If you call into other people's code and don't know what they might throw in a future version of their code, you can catch that with a catchall function.

exception.Try(func(){
	// something that might go wrong
    exception.Throw(fmt.Errorf("Some error"))
}).CatchAll(func(e interface{}){
	log.Warningf("An error occured: %+v", e)
}).Go()

panic

Sometimes you have some code that calls panic.

exception.Try(func(){
	// something that might go wrong
    panic(fmt.Errorf("Some error"))
}).CatchAll(func(e interface{}){
	log.Warningf("An error occured: %+v", e)
}).Go()

ignore

You just want to try over and over?

stop := false
for !stop {
	exception.Try(func(){
		// something that might go wrong
	    panic(fmt.Errorf("Some error"))
        stop = true
	}).Ignore().Go()
}

finally

If you need to run some cleanup code anyways

exception.Try(func(){
	// something that might go wrong
    panic(fmt.Errorf("Some error"))
}).Ignore().Finally(func(){
	// some cleanup
})

About

exception handling for golang

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages