Stories
Slash Boxes
Comments

SoylentNews is people

posted by martyb on Wednesday July 17 2019, @05:37PM   Printer-friendly
from the No!-Try-not.-Do,-or-do-not.-There-is-no-try.-YODA dept.

The Go language will not be adding a "try" keyword in the next major version, despite this being a major part of what was proposed for version 1.14.

Go, an open source language developed by Google, features static typing and native code compilation. It is around the 15th most popular language according to the Redmonk rankings.

Error handling in Go is currently based on using if statements to compare a returned error value to nil. If it is nil, no error occurred. This requires developers to write a lot of if statements.

"In general Go programs have too much code-checking errors and not enough code handling them," wrote Google principal engineer Russ Cox in an overview of the error-handling problem in Go.

A try statement was proposed to help reduce the coding burden. Upon further reflection:

That proposal has now been abandoned. Robert Griesemer, one of the original designers of Go, announced the decision in a post yesterday.

[...] “Making an exit point of a function that isn't a return, and is meant to be commonplace, may lead to much less readable code,” said one user.

The outcome is a good one insofar as the Go community has proved able to make and withdraw a major proposal without rancour. And as for error handling, no doubt the team will, um, try again.


Original Submission

 
This discussion has been archived. No new comments can be posted.
Display Options Threshold/Breakthrough Mark All as Read Mark All as Unread
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
  • (Score: 2) by The Mighty Buzzard on Wednesday July 17 2019, @10:51PM (2 children)

    by The Mighty Buzzard (18) Subscriber Badge <themightybuzzard@proton.me> on Wednesday July 17 2019, @10:51PM (#868268) Homepage Journal

    Code legibility. Either method works just fine but having both of them potentially in the same file is a Perl-like headache waiting to happen. Handling by "if" is already in all the code and it is what people have learned to expect and use in Go, so "try" needs to fuck off.

    --
    My rights don't end where your fear begins.
    Starting Score:    1  point
    Karma-Bonus Modifier   +1  

    Total Score:   2  
  • (Score: 2) by janrinok on Thursday July 18 2019, @08:07AM (1 child)

    by janrinok (52) Subscriber Badge on Thursday July 18 2019, @08:07AM (#868408) Journal

    It depends on what you mean by legibility. In Python you have the try:except:finally, with the latter being entirely optional. But it is very useful for cleaning up whether an error occurs or not. For example, if you are working with files you might want to ensure that all files are closed cleanly in either case. So:

    f = open(somefileForWriting)
    try:
        theResults =  doSomething()
        f.write(theResults)
    except unexpectedError:
       f.write(errorMessageToFile)
    finally:
        f.close()

    I'm sure each language has its equivalent but I find the Python way clean and it keeps the structure easy to read and understand. There are also better ways of writing the above code in Python, but I've kept it this way to help those who are not used to Python code.