Go - No try..catch


chess_rock
Member
Registered: 19.11.11 22:52
Timezone: UTC +0
Posts: 16

Hello everyone,

I've been reading a lot about golang, the programming language created by google, and i have a huge point i'd like to discuss with you.

I've realized it has no try..catch blocks or assertions and if you want to deal with exceptions you have to create if conditions for every situation! Even the print function returns error messages...

The creators of the language defend the point that it is a distributed language and that having try..catch is complicated because you lose control on debug, but i've seen python and it deals really well with its stack trace handling on distributed cases.

What do you think of that? Do you think if..else takes care better of parallel situations than try..catch? Don't you think that such a modern language should have modern functionalities too?


chess_rock
Member
Registered: 19.11.11 22:52
Timezone: UTC +0
Posts: 16

Hello everyone,

I've been reading a lot about golang, the programming language created by google, and i have a huge point i'd like to discuss with you.

I've realized it has no try..catch blocks or assertions and if you want to deal with exceptions you have to create if conditions for every situation! Even the print function returns error messages...

The creators of the language defend the point that it is a distributed language and that having try..catch is complicated because you lose control on debug, but i've seen python and it deals really well with its stack trace handling on distributed cases.

What do you think of that? Do you think if..else takes care better of parallel situations than try..catch? Don't you think that such a modern language should have modern functionalities too?


ynori7
Administrator
Registered: 24.08.11 12:16
Timezone: UTC +2
Posts: 144

For anyone who hasn't seen it (like me a few minutes ago) here's what Go's error handling looks like:

if err := api.Func(); err != nil {
    // handle err
    fmt.Println("err: " + err.String())
}

It makes a call to the function and saves the result as 'err'. Then it checks if it's not null (I dunno why they changed the standard "null" to "nil" ).

Do you have to create a check for each situation or can you just make a function call and then ignore it?

Or did you mean that you have to make a separate try-catch type of thing for each exception instead of wrapping it all in one? Because I think it could be possible to do that with something like this (I dont know if this is valid Go code, but I'm assuming it is):

err1 = api.Func1();
err2 = api.Func2();
err3 = api.Func3();
if  err1 != nil | err2 != nil | err3 != nil {
    // handle err
}

Similar python code would look something like:

try:
  func1()
  func2()
  func3()
except:
  #handle exception

I think the python way is a little cleaner and more natural, but I guess the Go way gives the advantage that you know exactly which function call caused which error.

i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg


ynori7
Administrator
Registered: 24.08.11 12:16
Timezone: UTC +2
Posts: 144

For anyone who hasn't seen it (like me a few minutes ago) here's what Go's error handling looks like:

if err := api.Func(); err != nil {
    // handle err
    fmt.Println("err: " + err.String())
}

It makes a call to the function and saves the result as 'err'. Then it checks if it's not null (I dunno why they changed the standard "null" to "nil" ).

Do you have to create a check for each situation or can you just make a function call and then ignore it?

Or did you mean that you have to make a separate try-catch type of thing for each exception instead of wrapping it all in one? Because I think it could be possible to do that with something like this (I dont know if this is valid Go code, but I'm assuming it is):

err1 = api.Func1();
err2 = api.Func2();
err3 = api.Func3();
if  err1 != nil | err2 != nil | err3 != nil {
    // handle err
}

Similar python code would look something like:

try:
  func1()
  func2()
  func3()
except:
  #handle exception

I think the python way is a little cleaner and more natural, but I guess the Go way gives the advantage that you know exactly which function call caused which error.

i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg


chess_rock
Member
Registered: 19.11.11 22:52
Timezone: UTC +0
Posts: 16

Do you have to create a check for each situation or can you just make a function call and then ignore it?
Or did you mean that you have to make a separate try-catch type of thing for each exception instead of wrapping it all in one?

That's a problem with Go... You can't wrap all errors into a single catch like we could do with languages like python for example. You have to create checks for every situation, and if you miss one check, the error might occur (even if it is a rare possibility) and there is nothing you can do.

It is possible to put all conditions into a single if statement, but if you miss one condition you'll have an error anyways.

I think the python way is a little cleaner and more natural, but I guess the Go way gives the advantage that you know exactly which function call caused which error.

Well, python is quite verbose when exceptions occur, and if you need detailed information about a specific type of exception, you can always use hierarchies of Exception classes to have a more descent information about the error. I have never experienced problems finding the function that had an error using multiple processes' applications. I think Go could have thought of a better solution rather than using if statements for every single possible exception.

I don't know if i'm criticizing too harshly, but i cannot see big advantages of using that type of error handling Google chose for golang. Am I saying stupidities here? whoa


chess_rock
Member
Registered: 19.11.11 22:52
Timezone: UTC +0
Posts: 16

Do you have to create a check for each situation or can you just make a function call and then ignore it?
Or did you mean that you have to make a separate try-catch type of thing for each exception instead of wrapping it all in one?

That's a problem with Go... You can't wrap all errors into a single catch like we could do with languages like python for example. You have to create checks for every situation, and if you miss one check, the error might occur (even if it is a rare possibility) and there is nothing you can do.

It is possible to put all conditions into a single if statement, but if you miss one condition you'll have an error anyways.

I think the python way is a little cleaner and more natural, but I guess the Go way gives the advantage that you know exactly which function call caused which error.

Well, python is quite verbose when exceptions occur, and if you need detailed information about a specific type of exception, you can always use hierarchies of Exception classes to have a more descent information about the error. I have never experienced problems finding the function that had an error using multiple processes' applications. I think Go could have thought of a better solution rather than using if statements for every single possible exception.

I don't know if i'm criticizing too harshly, but i cannot see big advantages of using that type of error handling Google chose for golang. Am I saying stupidities here? whoa


ynori7
Administrator
Registered: 24.08.11 12:16
Timezone: UTC +2
Posts: 144

Maybe they wanted to give a little more flexibility in the error handling. Like this:

err = api.Func1();
if  err == nil & someCondition {
    // no error, do some stuff
} else if err != nil & someCondition {
    // handle err in some way
} else if err != nil{
    // handle err in other way
}

You could just put some if statements inside a catch block too to do that, but maybe there exists some advantage to doing it this way.

i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg


ynori7
Administrator
Registered: 24.08.11 12:16
Timezone: UTC +2
Posts: 144

Maybe they wanted to give a little more flexibility in the error handling. Like this:

err = api.Func1();
if  err == nil & someCondition {
    // no error, do some stuff
} else if err != nil & someCondition {
    // handle err in some way
} else if err != nil{
    // handle err in other way
}

You could just put some if statements inside a catch block too to do that, but maybe there exists some advantage to doing it this way.

i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg