Skip to content

proposal: Go 2: catch error handler #43777

Closed
@dmitryuck

Description

@dmitryuck

Would you consider yourself a novice, intermediate, or experienced Go programmer?
novice

What other languages do you have experience with?
JS, C#, Dart

Would this change make Go easier or harder to learn, and why?
I believe easier

Has this idea, or one like it, been proposed before?
some similar ideas of course were, but this different

If so, how does this proposal differ?

Who does this proposal help, and why?
help to handle errors in a more convenient way, increase performance by using less memory

What is the proposed change?
error handling

Please describe as precisely as possible the change to the language.
adding throw catch functionality
catch works like defer but only in case when some function in scope throws an exception
the scope of the catch block is function scope
it could be a set of catch blocks
catch block runs only if the code in func scope throws an error

What would change in the language spec?
adding throw, catch keywords

Please also describe the change informally, as in a class teaching Go.
if the function throws an error then the next catch block can handle it

Is this change backward compatible?
no, the old way should not conflict with this approach

Breaking the Go 1 compatibility guarantee is a large cost and requires a large benefit.
do not break

Show example code before and after the change.
before:

func SomeFunc() result, error {
  return nil, "error here"
}

func main() {
  res, err := SomeFunc()
  if err != nill {
    log.Println(err, "some error occur");
  }
}

after:

func SomeFunc() {
  throw "Error in SomeFunc"
}


func main() {
  result := SomeFunc()

  catch func(e error) {
    log.Println("some error occur");
  }
}

or:

func SomeFunc() {
  throw "Error in SomeFunc"
}

func ErrorHandler(e error) {
  log.Println(e, "some error occur");
}

func main() {
  result2 := SomeFunc()
  catch ErrorHanler
}

or:

func SomeFunc_1() {
  throw ERROR_TYPE_1
}

func SomeFunc_2() {
  throw ERROR_TYPE_2
}

func SomeFunc_3() {
  throw ERROR_TYPE_3
}

func ErrorHandler(e error) {
  switch(e) {
    case ERROR_TYPE_1: DoSomething()
    case ERROR_TYPE_2: DoSomething()
    case ERROR_TYPE_3: DoSomething()
  }

  log.Println(e, "some error occur");
}


func main() {
  result1 := SomeFunc_1()
  result2 := SomeFunc_2()
  result3 := SomeFunc_3()

  catch ErrorHanler
}

What is the cost of this proposal? (Every language change has a cost).
benefits of having this approach :

  • no return error from function,
  • no error declaration in return parameters,
  • no error variable declaration in function calls scope,
  • no code like if err != nill

How many tools (such as vet, gopls, gofmt, goimports, etc.) would be affected?
none

What is the compile time cost?
possible internal optimization as a new mechanism

What is the run time cost?
possible internal optimization as a new mechanism

Can you describe a possible implementation?
sure

Do you have a prototype? (This is not required.)
code above

How would the language spec change?
a bit

Orthogonality: how does this change interact or overlap with existing features?
does not conflict

Is the goal of this change a performance improvement?
performance improvements also

If so, what quantifiable improvement should we expect?
an application could use less memory

How would we measure it?
depends on implementation

Does this affect error handling?
yes

If so, how does this differ from previous error handling proposals?
because not necessary to create a new variable err every time, performance increases

Is this about generics?
nope

If so, how does this differ from the current design draft and the previous generics proposals?
don't know

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions