Background... c++/python programmer who sees some good things in GO but not impressed with the error handling. I think there are often clear cut cases where some code doesn't want the burden of checking and re-raising errors, and some that do. The some that do would be top level functions and data structures needing to keep internal state clean. The some that definitely don't would be short scripts.However I vaguely agree (or am neutral) on the issue of exception catching syntax being ugly control flow.My idea is function calls can have attributes. I can either do:# If error, raise an exception and someone else will handle it or notbuffer := socket.recv(100)file.close()or:# Obj can be an exception object or nil. Control flow is guaranteed
buffer, obj := socket.recv(100) _catchobj = file.close() _catchor perhaps this instead would be more syntax legal:buffer, obj := catch(socket.recv(100))obj = catch(file.close())or even one more idea:buffer, obj = socket.__recv(100)obj = file.__close()(its the same method, the leading __ just turns it into the catch version).obj can then be inspected with normal code constructs. catch() or _catch would make the last variable in the assignment list be the caught exception, or nil if no exception happened.I also think it would be cool if a code editor could automatically highlight all function calls that could have a non-local return (i.e. could raise an exception). Why make the programmer state in code / annotations what the compiler already knows? So one could audit a function with a lot of 'red' text vs a function with a lot of 'green' (does not raise exception).Just my two cents.- Karl
Here's an example:
http://play.golang.org/p/KQRqHjOSb5
You could also do the reverse and cast panics to errors.
However, as was mentioned, you're code will be way more robust if you check and handle errors as close to their source as possible and propagate them explicitly.