"if err, return it" is stack unwinding, done manually. That is precisely what it is. There is a special form of return where the normal thing you do is to just check if it's there, and if it is, return the same way.
And yes, you have to write code to throw exceptions. But with exceptions you DON'T have to write code to propagate them. With "if err, return err", you have to write that code EVERYWHERE to propagate your error returns, or they silently disappear.
C) Yes, exactly. Or, in any half-decent language, that's already done for you - an uncaught exception terminates the program. So you only need to write code to catch exceptions you can actually handle, and perhaps at a boundary (like a request handler in an HTTP server, where you want a generic "uncaught exception means log it and return a 500" handler).
The fundamental difference is the propagation, which takes exactly zero code with exception handling, but mandatory return value checks with manual stack unwinding.
And not just that you have to write it, but that you have to remember to write it. Go can't distinguish between choosing to suppress an error (e.g., because it's expected, or because you've handled it) and forgetting to propagate an error. The default is to propagate it in most languages because that's what you want 99% of the time.
Combined with consuming return values not being mandatory, it's super easy to call a function and not handle its error, and for that mistake to slip through code review.
Okay maybe in the purest definition it is, but you made it sound like you're manually reimplementing the actual stack unwinding. I.e. clearing up the stack frame when you return from a function. Which makes it sound like a lot more work than if (err) return;
I'm not saying don't use exceptions, because you absolutely should, for exceptional cases. I'm just saying you shouldn't use them entirely in replacement of regular error handling. An uncaught error in a http server to return 500 is exactly the kind of use case that makes sense.
Propagation of errors is nice, but I'd argue if your code is structured in a way that a non-exceptional error has to be propagated far up the call stack then your code is structured badly.
View all comments
Show parent comment
4
u/rosuav Sep 13 '23
"if err, return it" is stack unwinding, done manually. That is precisely what it is. There is a special form of return where the normal thing you do is to just check if it's there, and if it is, return the same way.
And yes, you have to write code to throw exceptions. But with exceptions you DON'T have to write code to propagate them. With "if err, return err", you have to write that code EVERYWHERE to propagate your error returns, or they silently disappear.
C) Yes, exactly. Or, in any half-decent language, that's already done for you - an uncaught exception terminates the program. So you only need to write code to catch exceptions you can actually handle, and perhaps at a boundary (like a request handler in an HTTP server, where you want a generic "uncaught exception means log it and return a 500" handler).
The fundamental difference is the propagation, which takes exactly zero code with exception handling, but mandatory return value checks with manual stack unwinding.