Created
October 4, 2012 16:02
-
-
Save losinggeneration/3834606 to your computer and use it in GitHub Desktop.
A quick and dirty exception handler for the Go programming language
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
MIT license: | |
Copyright (c) 2012 Harley Laue <[email protected]> | |
Permission is hereby granted, free of charge, to any person obtaining a | |
copy of this software and associated documentation files (the | |
"Software"), to deal in the Software without restriction, including | |
without limitation the rights to use, copy, modify, merge, publish, | |
distribute, sublicense, and/or sell copies of the Software, and to | |
permit persons to whom the Software is furnished to do so, subject to | |
the following conditions: | |
The above copyright notice and this permission notice shall be included | |
in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | |
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
/* | |
-------------------------------------- | |
*/ | |
/* | |
As a general note, I do not recommend using this. If you do choose to use it, please make sure everything is properly handled and only use it internally. | |
*/ | |
package except | |
// Return true if exception was handled and false to continue panicing | |
type Catchable func(e error) bool | |
// f is the function to try, and c lets us know if the exception was properly handled | |
func Try(f func(), c Catchable) { | |
func() { | |
defer catch(c) | |
f() | |
}() | |
} | |
// Throw an exception | |
func Throw(e error) { | |
panic(e) | |
} | |
func catch(c Catchable) { | |
if err := recover(); err != nil { | |
if e, ok := err.(error); ok { | |
if r := c(e); r == false { | |
// Error not handled. Panicing anyway | |
panic(e) | |
} | |
} else { | |
// Panicing anyway | |
panic(e) | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// an example using the except library | |
package main | |
import ( | |
"errors" | |
"except" | |
"fmt" | |
"time" | |
) | |
var ( | |
failError = errors.New("Fail!") | |
fubarError = errors.New("Fubar!") | |
) | |
func a() { | |
fmt.Println("a()") | |
} | |
func b() { | |
fmt.Println("b()") | |
except.Throw(failError) | |
} | |
func c() { | |
fmt.Println("c()") | |
} | |
func main() { | |
fmt.Println("This try will be caught and continue") | |
except.Try(func() { | |
a() | |
b() | |
c() | |
}, func(e error) bool { | |
if e == failError { | |
return true | |
} | |
return false | |
}) | |
time.Sleep(1 * time.Second) | |
fmt.Println("This try is going to fail") | |
except.Try(func() { | |
a() | |
b() | |
c() | |
}, func(e error) bool { | |
if e == fubarError { | |
return true | |
} | |
return false | |
}) | |
time.Sleep(1 * time.Second) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment