Skip to content

Instantly share code, notes, and snippets.

@jordanorelli
Created November 28, 2012 18:00
Show Gist options
  • Save jordanorelli/4162895 to your computer and use it in GitHub Desktop.
Save jordanorelli/4162895 to your computer and use it in GitHub Desktop.
this error handling pattern isn't obvious at first
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