Created
November 28, 2012 18:00
-
-
Save jordanorelli/4162895 to your computer and use it in GitHub Desktop.
this error handling pattern isn't obvious at first
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
func f() error { | |
// might return an error | |
} | |
func somethingElse() { | |
switch err := f(); err { | |
case mgo.ErrNotFound: | |
// a specific error that is recoverable | |
case nil: | |
// no error occurred. Usually this is just a break statement. | |
default: | |
// all other errors get handled here. This is where you want err to be named, so that you can reference it to either return it or write it to a log file. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment