Documentation
¶
Overview ¶
Package catch is an experiment to see if a concise way to work with panics will break Go's convention to explicitly check for errors as described in https://blog.golang.org/error-handling-and-go. By no means it is a good idea to work against language design. So this is to be handled with care!
Somewhere I read a good advice regarding Go's panic:
Do not expect someone else to recover from your panic!
This is quite in line with the “Don't panic across package boundaries” rule. If you panic across package boundaries you are responsible for the program to crash. This shall be something truly exceptional!
Consider that, when my C++ compiler started to support exception handling (yes exceptions weren't there forever), they explained that C++ exceptions shall represent some truly exceptional program state. But today – also in Java – exceptions are rather common (“exception” / “common” - think about it). They are used for error handling. IMHO Andrei Alexandrescu elaborates on this very nicely in https://www.youtube.com/watch?v=kaI4R0Ng4E8.
Addendum to the license ¶
You must not use this package before you really understand the above advice.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RecoverAs ¶ added in v0.2.0
func RecoverAs(err *error)
RecoverAs is used as a deferred function to return a recovered panic as a Recovered error.
Example ¶
type p struct { t time.Time msg string } err := func() (err error) { defer RecoverAs(&err) panic(p{ t: time.Date(1971, 12, 24, 18, 0, 0, 0, time.UTC), msg: "Don't panic!", }) }() fmt.Println(err)
Output: panic: catch.p={t:{wall:0 ext:62198042400 loc:<nil>} msg:Don't panic!}