Re:The Go Programming Language, or: Why all C-like languages...
by klon on Thursday March 08, 2012 @06:28PM
(#3682222)
Ok but exceptions also give you alot of context about the error like exactly where it occured. How is that handled in Go?
Re:The Go Programming Language, or: Why all C-like languages...
by ericbb on Thursday March 08, 2012 @09:47PM
(#3682679)
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.
Re:The Go Programming Language, or: Why all C-like languages...
by casualsuperman2 on Friday March 09, 2012 @06:23PM
(#3686427)
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.
Re:The Go Programming Language, or: Why all C-like languages...
by dhconnelly on Thursday March 08, 2012 @09:06PM
(#3682594)
Do you mean that exceptions, when they bring down your program, indicate the line where the exception was thrown? If so, then `panic` in Go does the same thing.
Using goroutines and panic/recover, you can do the same things you do with try/catch/throw.
Re:The Go Programming Language, or: Why all C-like languages...
by barrkel on Friday March 09, 2012 @02:26AM
(#3683278)
No; the point is that the information is contextual. For a database failure, the information will be DB specific; for an IO failure, IO specific; for an IO failure in the DB subsystem, it will be information about IO failure wrapped in info about the DB failure.
I think too many people get wrong idea about exceptions from defaults in many environments. Exceptions being thrown should not normally be a sign of a bug; knowing the line number the exception thrown should be irrelevant information almost all the time, save for errors like access violations or null pointer exceptions.
Instead, the information contained in an exception can usually be turned into actionable data to a user or administrator of an app (depending on whether it's on the client or server).
Hope this clears some things up.