News for hackers. Stuff that matters.
Re:Don't just check errors, handle them gracefully (2016) by carlio on Tuesday June 03, 2025 @04:19PM (#44174248)
"Something went wrong. Please try again".
Re:Don't just check errors, handle them gracefully (2016) by vel0city on Tuesday June 03, 2025 @04:17PM (#44174228)
Gotta love reading a new to you codebase and seeing a bunch of

    catch (Exception ex) { };
littered throughout. Fixed the bug boss, close that ticket!
Re:Don't just check errors, handle them gracefully (2016) by marklubi on Tuesday June 03, 2025 @04:26PM (#44174342)
Totally agree with finding the source of the bug (there are some that I've spent weeks tracking down, there are millions of people that have my fixed code on their devices).

If you've going to catch an error with a noop, there better be a comment explaining why you're not doing anything. It's not often, but sometimes, failing gracefully is just swallowing the error and moving on.

Re:Don't just check errors, handle them gracefully (2016) by xboxnolifes on Tuesday June 03, 2025 @05:01PM (#44174687)
At minimum, the error should be logged somewhere, and not just vanished. At least you get to know how frequently it occurs if/when that becomes meaningful to you.
Re:Don't just check errors, handle them gracefully (2016) by vel0city on Tuesday June 03, 2025 @05:58PM (#44175219)
Exactly this. Log it, maybe at some really verbose log level, but please let me know it is hitting this point and a little bit of information of what the exception was.

Also, only do it for exceptions you think might actually hit there and you're OK with quietly squashing. Otherwise, please let something else try and handle it.

Re:Don't just check errors, handle them gracefully (2016) by marklubi on Wednesday June 04, 2025 @09:37AM (#44180602)
In an app, if you have a string of network requests to your own services, there's no particular point in logging multiple (or any) 'no internet connection' errors. They won't make it to you, and having those doesn't provide much value. That's an end user problem.

I have plenty of monitors to ensure my services are available (four-nines uptime for almost a decade). Internet unavailable errors are useless to log.

Re:Don't just check errors, handle them gracefully (2016) by lapetitejort on Tuesday June 03, 2025 @05:16PM (#44174826)
I struggled with this recently. Someone was trying to patch up an error and force the program to continue. I asked him what was causing the error and he could only respond with how they were trying to patch it. I got so frustrated that I took on the error myself and fixed it with two lines of code.

I think the problem is that some people think errors are supposed to happen. Just a cost of doing business. They can't be fixed, only mitigated.

Re:Don't just check errors, handle them gracefully (2016) by sidewndr46 on Tuesday June 03, 2025 @05:34PM (#44174987)
On Error Resume Next

The single line that magically fixes all Visual Basic 6 applications bugs

Re:Don't just check errors, handle them gracefully (2016) by layer8 on Tuesday June 03, 2025 @05:51PM (#44175150)
Bash does it even by default. ;)
Re:Don't just check errors, handle them gracefully (2016) by tshaddox on Tuesday June 03, 2025 @05:11PM (#44174793)
Sometimes you're stuck using some API that throws exceptions for expected (i.e. non-exceptional) errors, and you really do want to do nothing additional when those expected errors occur.
Re:Don't just check errors, handle them gracefully (2016) by devman0 on Tuesday June 03, 2025 @05:42PM (#44175071)
This is, in theory, the difference between checked and unchecked exceptions in languages that support such distinctions. Checked exceptions are basically part of the control flow and the API contract used for things that can happen but are not necessary programmer errors (IO Exceptions being the classic example). Whereas unchecked exceptions are supposed to be used for programming errors (think Illegal Arguments, Null Pointer, Array Index Out of Bounds). Unchecked also usually cover other fatal conditions like stack overflow or OOM. Checked, handle with care, Unchecked, let the stack unwind and fail fast.
Re:Don't just check errors, handle them gracefully (2016) by vel0city on Tuesday June 03, 2025 @05:53PM (#44175172)
Sure, I agree. I'd still prefer you properly scope it to just those expected exceptions, maybe put in some kind of debug level log related to it. The above code grabs every kind of exception and silently squashes it into oblivion.
Re:Don't just check errors, handle them gracefully (2016) by Groxx on Tuesday June 03, 2025 @05:11PM (#44174795)
especially Exception. Ugh. Gotta love those interrupt-ignorers.
Re:Don't just check errors, handle them gracefully (2016) by layer8 on Tuesday June 03, 2025 @05:53PM (#44175176)
Unfortunately, it’s somewhat of a lost cause unless you control all the code. Even some OJDBC drivers swallow interrupts. I had to resort to application-specific interrupt flags.
Re:Don't just check errors, handle them gracefully (2016) by atoav on Tuesday June 03, 2025 @05:00PM (#44174665)
Handling exceptions is interesting because in my experience a lot of it boils down to a social dimension:

* if the input data field for a telephone number contains a country name do you communicate that error back to the source, if yes how and how often (assuming your source isn't a life user bit a bureaucratic institution)

* if there is an error that really shouldn't happen, how will you notice? You probably don't have all the logs scrolling on 100 displays to watch for the red. So maybe send emails? But if you send emails about pointless stuff people will start to ignore them

Re:Don't just check errors, handle them gracefully (2016) by eknkc on Tuesday June 03, 2025 @05:51PM (#44175148)
Having a lint rule to disallow empty catch bodies has been a good practice for me. Sometimes you deliberately do it and even then I am forced to at least leave a `// ignore` there.
Re:Don't just check errors, handle them gracefully (2016) by vel0city on Wednesday June 04, 2025 @01:42PM (#44183431)
Absolutely, linter rules that expose such things are critical to fixing these things. And I add them to the PR linter to comment on PRs when I can to point out discussions about such things.
Re:Don't just check errors, handle them gracefully (2016) by scrame on Tuesday June 03, 2025 @04:38PM (#44174460)
this completely happened to me, but with a comment:

// this never happens

... guess what happened?

/happenings

Re:Don't just check errors, handle them gracefully (2016) by layer8 on Tuesday June 03, 2025 @05:59PM (#44175232)
In every project I have an unchecked CantPossiblyHappenException type (usually with a better name ;)) that is thrown in such cases. This documents that the developer thought this condition can’t occur, while still escalating it should it nevertheless occur.
Re:Don't just check errors, handle them gracefully (2016) by avg_dev on Tuesday June 03, 2025 @03:28PM (#44173693)
https://go.dev/blog/go1.13-errors

I wouldn't use the advice in this article. It's quite old and the stdlib now has support for new stuff via error wrapping.

Re:Don't just check errors, handle them gracefully (2016) by pixelpoet on Tuesday June 03, 2025 @04:12PM (#44174183)
I like my Go proverbs to be at least 100 years old: https://en.wikipedia.org/wiki/Go_proverb
Re:Don't just check errors, handle them gracefully (2016) by anttiharju on Tuesday June 03, 2025 @03:30PM (#44173713)
Posted this because of the other Go error handling post today https://news.ycombinator.com/item?id=44171677

I like error wrapping. I've worked in codebases where systems had to be debugged by piecing together individual log lines. With error wrapping I can usually see the exact path that lead to the error in one line.

Re:Don't just check errors, handle them gracefully (2016) by glenjamin on Tuesday June 03, 2025 @03:34PM (#44173760)
It would be cool if someone could come up with a way for the runtime system to automatically wrap errors with the calling context.

That way you’d be able to trace the stack of functions that were involved in passing an error back up to the place where it’s actually handled.

Re:Don't just check errors, handle them gracefully (2016) by Sarkie on Tuesday June 03, 2025 @04:13PM (#44174197)
It's a bold idea cotton
Re:Don't just check errors, handle them gracefully (2016) by __s on Tuesday June 03, 2025 @04:38PM (#44174456)
It's possible, stdlib just doesn't do it

https://pkg.go.dev/github.com/pkg/errors

Re:Don't just check errors, handle them gracefully (2016) by elcritch on Tuesday June 03, 2025 @04:44PM (#44174521)
It's sarcasm I believe, they used to call this exceptions. ;)
Re:Don't just check errors, handle them gracefully (2016) by danesparza on Tuesday June 03, 2025 @04:04PM (#44174099)
"With error wrapping I can usually see the exact path that lead to the error in one line"

This is called a stack trace, and I agree it can be very useful (if the developers were thoughtful about their error handling)

Re:Don't just check errors, handle them gracefully (2016) by eschatology on Tuesday June 03, 2025 @04:16PM (#44174223)
Stack trace is much more verbose and shows the symbols leading to the failing function call The error wrapping(s) produce a log line containing a brief message from each layer in the codebase that expected no errors — subtle difference but one is a dump and the other is much more meaningful.
Re:Don't just check errors, handle them gracefully (2016) by kubanczyk on Tuesday June 03, 2025 @05:02PM (#44174700)
> The error wrapping(s) produce a log line containing a brief message from each layer in the codebase that expected no errors — subtle difference

Agreed. How I articulate it, often a function is just another layer, does one core thing and one-two extras. I wrap meticulously the errors of the extras. The core errors mainly speak for themselves, so they rarely need any wrapping.

Avoids:

    cannot load config:  cannot load config: cannot load config: file not found
But promotes:

    cannot load config: cannot connect to Configurer: loading client cert: PEM invalid
The latter case reads like a list of plot twists, because it is one. A corresponding 40-line stack trace might be less readable.
Re:Don't just check errors, handle them gracefully (2016) by anttiharju on Tuesday June 03, 2025 @04:09PM (#44174159)
I suppose I phrased this poorly. Stack traces are useful indeed. I guess what I like that usually wrapped errors are bit more thoughtful, stack traces can look a bit like "vomit".
Re:Don't just check errors, handle them gracefully (2016) by RSHEPP on Tuesday June 03, 2025 @04:26PM (#44174343)
from my experience of late, engineers usually toss stack traces into LLMs and ask what the issue is. good error wrapping, engineers grep for one of the first errors and get to the error code right away. both ways work.
Re:Don't just check errors, handle them gracefully (2016) by sfn42 on Tuesday June 03, 2025 @06:04PM (#44175278)
If you can't read a stack trace without an LLM you're not an engineer.
Re:Don't just check errors, handle them gracefully (2016) by danesparza on Thursday June 05, 2025 @01:27PM (#44193790)
Counterpoint: You might also just be working on an exceptionally bad codebase or with an exceptionally bad runtime (I'm looking at you C/C++ or minified Javascript)
Re:Don't just check errors, handle them gracefully (2016) by sfn42 on Friday June 06, 2025 @09:20AM (#44200588)
Sure, if the stack trace doesn't have any useful information then it's not useful.

In my experience it's usually useful though, it usually tells you both what the problem is and where it is. Sadly a lot of people just don't even try to read them.

Re:Don't just check errors, handle them gracefully (2016) by datadrivenangel on Tuesday June 03, 2025 @03:26PM (#44173671)
Software should fall over quickly. Graceful error handling can hide many issues.
Re:Don't just check errors, handle them gracefully (2016) by markwaldron on Tuesday June 03, 2025 @04:07PM (#44174135)
My take on this post is that things will break, but they should tell you they broke in an actionable and somewhat standardized way. If the only message that shows up is "An error has occured" that doesn't help the user, especially if they are non-technical.
Re:Don't just check errors, handle them gracefully (2016) by ninetyninenine on Tuesday June 03, 2025 @04:05PM (#44174112)
I love the go philosophy of errors being silent by default. It forces us developers to handle things gracefully. Additionally no stack traces along with the errors which is good because devs are too reliant on stack traces nowadays.
Re:Don't just check errors, handle them gracefully (2016) by altruios on Tuesday June 03, 2025 @05:00PM (#44174672)
This sounds like it needs an /s.

The complaint of developers "being reliant on stack traces" tone sounds an awful lot like: "real programmers just use punch cards and ignore any tooling we've come up with in the last 70 years: just use your brain"

Re:Don't just check errors, handle them gracefully (2016) by ninetyninenine on Tuesday June 03, 2025 @06:26PM (#44175447)
It’s not sarcasm. This is just my personal philosophy. I feel attacked.
Re:Don't just check errors, handle them gracefully (2016) by altruios on Wednesday June 04, 2025 @06:54PM (#44186443)
I speak in hyperbole here. I only point out the giants whos shoulders we stand on left us fantastic tools to use.

There is something to the philosophy of keeping your interface 'dark' such that you learn to 'shine'. But that's something I ascribe to a personal development journey.

Re:Don't just check errors, handle them gracefully (2016) by 3cats-in-a-coat on Tuesday June 03, 2025 @03:47PM (#44173914)
"Handling errors gracefully" doesn't happen at the handler. It starts at the execution model and your architecture. If a component can "bomb" in an isolated way, then the client that invoked that component can handle that gracefully.

This means things like durability, consistency, atomicity, and isolated side-effects (if any).

This is what Erlang does. A process is isolated, so when it just falls apart, the parent can handle that "gracefully" despite said process literally disintegrated in a critical irreparable way.

It's a bit like designing covert spy networks...

Re:Don't just check errors, handle them gracefully (2016) by 827a on Tuesday June 03, 2025 @04:42PM (#44174497)
For a language that prides itself on its error handling, that's a whole lot of words written on the myriad of incorrect and bad ways to handle errors in the language, many of which are actively and irreversibly deployed in the standard library.
Re:Don't just check errors, handle them gracefully (2016) by neuroelectron on Tuesday June 03, 2025 @05:08PM (#44174764)
Best way to handle errors is to exit
Re:Don't just check errors, handle them gracefully (2016) by rollulus on Tuesday June 03, 2025 @05:16PM (#44174831)
This was a great article back then but Go’s errors package has changed since then quite a bit, with e.g. Is, As, and fmt.Errorf’s %w.
Re:Don't just check errors, handle them gracefully (2016) by Entwickler on Tuesday June 03, 2025 @05:34PM (#44174992)
Yeah, this has some general high level good advice, but is so old it doesn't include the new idiomatic ways of using errors.Is and error wrapping for error checking...