I think I disagree with everything here.
Exceptions Are Much Easier to Work With
Well, they’re “easier” in the same way that dynamic typing is easier. It’s obviously less work initially just to say “screw it; any error gets caught in
main()
”. But that’s short term easiness. In the long term its much more painful because:- You don’t know which functions might produce errors, and therefore you don’t know where you should be even trying to handle errors. (Checked exceptions are the answer here but they are relatively rarely used in practice.)
- Actually handling errors properly often involves responding to errors from individual function calls - at least adding human readable context to them. That is stupidly tedious with exceptions. Every line turns into 5. Sometime it makes the code extremely awkward:
try { int& foo = bar(); } catch (...) { std::cout << "bar failed, try ...\n"; return nullopt; } foo = 5;
(It actually gets worse than that but I can’t think of a good example.)
Over 100× less code! [fewer exception catching in their C++ database than error handling in a Go database]
Well… I’m guessing your codebase is a lot smaller than the other one for a start, and you’re comparing with Go which is kind of worst case… But anyway this kind of proves my point! You only actually have proper error handling in 140 places - apparently mostly in tests. In other words you just throw all exceptions to
main()
.System errors [he’s mainly talking about OOM, stack overflow & arithmetic errors like integer overflow]
Kind of a fair point I guess. I dunno how you can reasonably stack overflows without exceptions. But guess what - Rust does have
panic!()
for that, and you can catch panics. I’d say that’s one of the few reasonable cases to usecatch_unwind
.Exceptions Lead to Better Error Messages
Hahahahahaha. I dunno if a bare stack trace with
NullPointerException
counts as a “better error message”. Ridiculous.Exceptions Are More Performant
Sure maybe in error handling microbenchmarks, or particularly extreme examples. In real world code it clearly makes little difference. Certainly not enough to choose an inferior error handling system.
I would say one real reason to prefer exceptions over
Result<>
s is they are a fair bit easier to debug because you can just break on throw. That’s tricky withResult<>
because creating aErr
is not necessarily an error. At least I have not found a way to “break onErr
”. You can break onunwrap()
but that is usually after the stack has been unwound quite a bit and you lose all context.It can be pretty convenient to throw an error and be done with it. I think for some languages like Python, that is pretty much a prefered way to deal with things.
But the entire point of Rust and Result is as you say, to handle the places were things go wrong. To force you to make a choice of what should happen in the error path. It both forces you to see problems you may not be aware of, and handle issues in ways that may not stop the entire execution of your function. And after handling the Result in those cases, you know that beyond that point you are always in a good state. Like most things in Rust, that may involve making decisions about using Result and Option in your structs/functions, and designing your program in ways that force correct use… but that a now problem instead of a later problem when it comes up during runtime.
But the entire point of Rust and Result is… to force you to make a choice of what should happen
Checked exceptions also force you to handle it and take way less boilerplate.
But nothing is forcing you to check exeptions in most languages, right?
While not checking for exceptions and .unwrap() are pretty much the same, the first one is something you get by not doing anything extra while the latter is entirely a choice that has to be made. I think that is what makes the difference, and in similar ways why for example nullable enabled project in C# is desired over one that is not. You HAVE to check for null, or you can CHOOSE to assume it is not by trying to use the value directly. To me it makes a difference that we can accidentally forget about a possible exception or if we can choose to ignore it. Because problems dealt with early at compile time, are generally better than those that happen at runtime.
I see your concern, but in practice that’s not what happens in languages like Java and Python with exceptions. Not checking for exceptions is a choice because everyone knows you need to check in your top-level functions. Forgetting to catch is a problem that only hits newbies.
A problem that only affects newbies huh?
Let’s say that you are writing code intended to be deployed headless in the field, and it should not be allowed to exit in an uncontrolled fashion because there are communications that need to happen with hardware to safely shut them down. You’re making a autonomous robot or something.
Using python for this task isn’t too out of left field, because one of the major languages of ROS is python, and it’s the most common one.
Which of the following python standard library functions can throw, and what do they throw?
bytes
,hasattr
,len
,super
,zip
The more I read about these kind of article the more I am amazed that our digital future is at hand in utterly incompetent people.
This person clearly have no understanding of monadic error (AKA Maybe/option monad or slightly more advanced Either monad), which is the first monad we teach at a class targeting second year undergrad.
The performance comparison is just plain factual error. The functional error code will continue to compute
n2
when computation ofn1
failed; the same do not happen in the exception version. If you compare codes with completely different traces, of course they will have different performance…A properly implemented monadic error will return as soon as compute for
n1
failed, and never execute the rest of the code. This is the default and idiomatic behavior in Haskell, OCaml, F#, and rust. This performance problem doesn’t even happen in LINQ-style handling like in C# and Kotlin (maybe also Typescript?).The point of monadic error is that its control flow is local, whereas exception is non-local. Specifically, the exception can be handled and occur anywhere in the code base, with no indication on the type level. So programmers will be constantly worrying about whether the exception in a function call is properly handled.
Even worse, when you try to catch a certain error, there is always the risk to accidentally catch similar exceptions in a library call or someone else’s code. Writing good code with try-catch requires a lot of principle and style guides. But unlike monads, these principle and rules cannot be enforced by the type system, adding extra burden to programmers.
In fact, we have known for a long time that non-local control flows (goto, break, contiune, exception, long jump) are the breeding ground for spaghetti code. As an evidence, many non-local control flows (goto, long jump) are baned in most languages.
That being said, there are certainly cases, with proper documentation, that exception style is easy to write and understand. But I think they are very specific scenarios, which have to be justified on a case-by-case basis.
One problem with exceptions is composability.
You have to rely on good and up-to-date documentation or you have to dig into the source code to figure out what exceptions are possible. For a lot of third party dependencies (which constitute a huge part of modern software), both can be missing.
Error type is a mitigation, but you are free to e.g. panic in Rust if you think the error is unrecoverable.
A third option is to have effect types like Koka, so that all possible exceptions (or effects) can be checked at type level. A similar approach can be observed in practical (read: non-academic) languages like Zig. It remains to be seen whether this style can be adopted by the mainstream.
The guy keeps on picking on Go, which is infamous for having terrible error handling, and then he has the nerve to even pick on the UNIX process return convention, which was designed in the 70s.
The few times he mentions Rust, for whatever reason he keeps on assuming that.unwrap()
is the only choice, which’s use is decidedly discouraged in production code.I do think there is room for debate here. But error handling is a hellishly complex topic, with different needs between among others:
- short- vs. long-running processes
- API vs. user-facing
- small vs. big codebase
- library vs. application code
- prototyping vs. production phase
And even if you pick out a specific field, the two concepts are not clearly separated.
Error values in Rust usually have backtraces these days, for example (unless you’re doing embedded where this isn’t possible).
Or Java makes you list exceptions in your function signature (except for unchecked exceptions), so you actually can’t just start throwing new exceptions in your little corner without the rest of the codebase knowing.
I find it quite difficult to properly define the differences between the two.