Skip to content

Comment on Comparing Go and Java, Part 2 – Performanceparent

Comments

Yes, exactly. If you find the boilerplate tedious, feel free to ignore errors completely. :) For test code it doesn't matter. It is the equivalent of providing no exception handler, or catching Exception or Throwable and doing nothing.

That is not optimal. It not going to crash in the location of first error. It will plough on a bit and crash somewhere else where the bad object/pointer was actually used to perform an illegal operation.

Something like a 'die on error' option might be useful for trivial scripts and applications.

When prototyping stuff is trivial to have a fail(err) function that panics in case err is not nil.

I sometimes do this, and then remove the fail() function to force myself to properly handle the errors. (Still, often is best to handle the errors as soon as you write the code anyway).

The key is that unlike with exceptions, the fact that you are ignoring the error is explicitly stated in the code, is not something that magically might happen.

And most importantly, errors are part of the documented API, with exceptions it is rarely documented what exceptions a function might throw, much less what exceptions the functions called by that function might throw.

Yes, Go error handling is a bit verbose, but that is a sign of how much better it is than exceptions, without falling into the 'checked exceptions' insanity.

Which is similar to the scenario where you ignore all exceptions and your test script got into a bad state. :)

Much as you could write this in Java:

    catch (Throwable t) {
      throw new RuntimeException("oh noes"); // or whatever
    }
You could write this in Go:
    if err != nil {
      panic("oh noes")
    }

But I can do the former for a whole block of code but have to do the later for each significant line of code.

They are are not the same.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.