9 ms·
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
by ericbb 15y ago
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.
- casualsuperman2 15y agoAnd 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.