Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The error type is an interface. You can return values that contain contextual information as long as the error value you return has an Error() method returning a string.

    type ParseError struct {
        line int
    }

    func (e ParseError) Error() string {
        return "Parse error at line: " + fmt.Sprint(e.line)
    }
Note: The current release may vary slightly on the details. I'm using a pre-release build. But older versions are similar.


And then in the code for handling the error, you can do

   switch err.(type) {
      case db.IOError:
          // Database exception here
      case io.FileNotFoundError:
          // File exception
      default:
          // Pass it to our caller for them to handle.
          return nil, err
   }
Which gives you the ability to selectively catch errors based on type.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: