Hacker News new | ask | show | jobs
by yati 2029 days ago
Panic/recover isn't used nearly as much for error handling in Go as in Java. An online resource being unreachable does not cause the net package functions to panic.
1 comments

Used or not, they ARE exceptions, and you need to write exception-safe code.

fmt.Print can throw. And HTTP handlers throwing exceptions is silently hidden. If you don't write code assuming anything can throw, then your code is broken.

And don't judge exception by how they are in Java. That's just a clusterfuck. No other language I'm aware of gets exceptions so wrong.

Panics Are exceptions but they should be seen as a fatal error. It is very discouraged from being used and the official doc insists that you should not use them for error handling. Qualifying a feature as exceptional use does count, just as having goto in C++ (and go) is possible but should be (and is) generally avoided should impact your perception of c++.
> Panics Are exceptions but they should be seen as a fatal error.

I agree. Well, "fatal" needs to be defined. If an HTTP handler throws an exception, is that fatal for the whole webserver?

Java seems crazy about this. Exceptions seems like it's being treated as just another return value. And that leads to a mess.

But C++? What parts of the C++ standard library have unreasonable exceptions used for errors? (there may be some, I just can't think of any)

And note that you have to throw (no pun intended) away large parts of the language if you remove exceptions. E.g. you can't have constructors without exceptions. How else would you signify "those arguments you gave to the constructor are no bueno".

Go doesn't have constructors, so it's consistent with what it says.

Also see my comment here, about how common or not, discouraged or not, the mere existence of exceptions in a language changes how you must write code to not have it be buggy: https://news.ycombinator.com/item?id=25275580

Yes, in C++ 'goto' is a code smell. It's not in C (greatly used for error handling), but C++ has RAII so `goto` should be rare outside of "clever" code (where "clever" is rarely good).

The mere fact that C++ doesn't have 'finally', and Java does, tells you a lot about how exceptions and RAII differs. If you write a macro for "finally" in C++ then you're doing it wrong.

Pretty much all of my `catch` clauses are in main() (or the root of an event handler), to pretty print the error and/or log to central service, or in a top level event like HTTP handler. `catch` should be about as common in C++ as `rescue` is in Go.

In Java it's fucking everywhere.

> If an HTTP handler throws an exception, is that fatal for the whole webserver?

Well, your handlers should not generally panic! It's called panic in Go so panicking should be hopefully rare for the peace of mind.

> Well, your handlers should not generally panic!

Uhm… no… no they should not.

I feel like you're missing the whole point, here. An interface that is extremely hard to use correctly without turning small bugs into major outages is not a good tool.

And one way to make sure this doesn't happen is to write exception-safe code, because Go has exceptions.

You could also argue that your C++ code shouldn't throw, and I agree. It should very rarely throw. But when it does it should be safe.

If Go had simply not had exceptions then this would have been easier.

> It's called panic in Go so panicking should be hopefully rare for the peace of mind

If you write a web service that hits a bug that panics about once per million requests, and you run 1000 qps, that means your Lock();dothing;Unlock() will deadlock the whole webserver once every 15 minutes.

If you write exception safe code, then it does not.

I was replying to the comment above -- Go or Rust did not simply rename exception related keywords, they have a completely different approach.

I don't know of any language that does mudane error handling with exceptions that is not a mess. C++, for instance, is extremely difficult to write exception safe code in.

> Go or Rust did not simply rename exception related keyword

I don't know about Rust, but Go most certainly did.

> C++, for instance, is extremely difficult to write exception safe code in.

It's WAY easier to write exception safe code in C++ than in Go, because C++ has RAII and scoped defers.

See my example for what a mess Go makes of this in this comment: https://news.ycombinator.com/item?id=25276360

I find C++ exception safe code to be pretty much trivial. Once you get used to "no naked resources" RAII just makes everything exception safe automatically.