On Mon, Dec 7, 2009 at 11:38 PM, Hong Zhang <
ho...@google.com> wrote:
> I think we are talking across each other already. I hope I don't
> confuse more, or you can correct it.
>
> If people don't like exception only because of syntax, we can do
> something like this:
>
> result, error = catch expression;
>
> This is almost the same as current code. The difference are:
> * os.Error becomes the exception type.
> * You don't need to declare os.Error as return value.
> * You write "throw err" instead of "return 0, err".
> * Errors are automatically propagated if not caught.
> * Minimum change to Go's current look and feel.
I think we are actually somewhat on the same page, then. I actually
don't mind that syntax you described that much, but I'd even be happy
with leaving things how they are, if it were possible to make the foo
[, bar] := baz() syntax in your own programs, or something to that
effect.
One of my biggest problems with Go as a language is that it doesn't
give you the facilities to do things that the language itself is able
to do. If you could define "exception" return values for your
functions and they could be propagated up the stack, I'd be quite
happy with that.
Say you had something like this:
func readFromDb(c *db.Conn) b *bytes,Buffer, e Exception {
}
Then, when you called the function, you could do:
buff, e := readFromDb(conn);
and you would catch the exception, but you could also do:
buff := readFromDb(conn);
if an exception occurred, then it would just be propagated up the
stack, perhaps to a place where a catch() was called. If there's no
proper handler, then it would abort the program.
This would be very much like the comma ok syntax for maps, channels,
etc. If you check for exception, you can catch it.
In this same vein, it really bothers me that the language is able to
support generics (channels and maps can work on any type), but there's
no facility to do the same sort of thing in your own code...
> In Go, unexpected exception kills the process (which is I don't like),
> so I am not sure what you discuss unexpected errors below. As
> far as I know, nil is the most common unexpected error.
I'm talking about unexpected errors in any app. For example, the
webapp I was describing has about 30 million users and does hundreds
of millions of page views a day. Obviously, any exceptional case that
can happen, does. Take the following: when reading from a file, you
might reach EOF and that's an expected error. That file might also be
hosted on NFS, though, and having the server go down would count as an
unexpected error and not the sort of thing that you can just recover
from in your function-- that's a textbook "exceptional" case that you
would want to stop execution, propagate up the stack and handle in a
very specific manner and you'd probably only want that handler code to
exist in one place.