Exceptions are often a better way to handle errors than returning them as values. We argue that traditional exceptions provide better user and developer experience, and show that they even result in faster execution.
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:
(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 use catch_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 with Result<> because creating a Err is not necessarily an error. At least I have not found a way to "break on Err". You can break on unwrap() 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.
There's also a massive tradeoff for when the error condition actually occurs. If an exception does get thrown and caught, that is comparatively slowwww.
The author pointed out how exceptions are often faster than checking every value. If your functions throws an error often enough that Exception handling noticeably slow down your program, surely you got to take a second look at what you're doing.
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.
But that's what I mentioned regarding Java there. Java calls them "exceptions", but generally forces the caller to either handle them or explicitly bubble them upwards...
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 of n1 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.
I was making a shared library at work and was recently asked to start throwing exceptions, because the users wouldn't care to check my returned error and just continue with the empty returned data.
Well, now they will most probably have an empty catch block and continue doing what they did before.
You have to explicitly check if the return value is an error and propagate it. You write the same boilerplate if (err) return err over and over again, which just litters your code.
That’s only true in crappy languages that have no concept of async workflows, monads, effects systems, etc.
Sad to see that an intentionally weak/limited language like Go is now the counterargument for good modeling of errors.
I naively thought it I may as well take a job using Go, as learning a new language is broadening, and some people like it, so lets find out first hand... I knew it was a questionable choice, looking at how Go adoption tailed off a while ago.
Turns out I hate Go. Sure it's better than C but that's a very low bar, and C was never a good alternative choice for the use cases I'm encountering. I'm probably suffering from a codebase of bad Go, but holy shit it's painful. So much silent propagation of errors up the stack so you never know where the origin of the error was. So very much boilerplate to expand simple activities into long unreadable functions. Various Go problems I've hit can be ameliorated if you "don't do it like that", but in the real world people "do it like that" all the time.
I'm really starting to feel like there are a lot of people in the company I've joined who like to keep their world obtuse and convoluted for job security.
I don't know the answer to your question, but I think that what is needed is just a bit of syntactic sugar, e.g. Rust has ? for returning compatible errors without looking into them. That seems to be powered by Try trait, that may be a monad, but I am not fluent enough to check if it formally is.
In Maybe monadic, its monadic bind will automatically resolves any failed computation, and don't need explicit checking.
for example, the code in Haskell looks something like the following:
fib: Int -> Int -> Maybe Int
fib max_depth idx =
do
guard (0 <= max_depth)
n1 <- fib (max_depth - 1) (idx - 1)
n2 <- fib (max_depth - 1) (idx - 2)
return (n1 + n2)
Haskell type class system automatically figures out this is a maybe monad, and check for error accordingly.
Notice, unlike the C code the author provide, this haskell code will exit immediately when n1 failed and never compute n2, similar to the behavior of the exception code. Thus I believe his point about performance is at least unjustified, if not wrong.
Another interesting fact about this code is that there is nothing that is built into the compiler/interpretor (except the do expression, which is just a minor syntactical sugar). For this code, the compiler designers don't need to design special semantics for raise and catch. Everything here, guard, return, and the Maybe monad (which is in charge of propagating errors) is defined by the user, using normal functions, no metaprogramming involved.
Wouldn't effect systems still be considered exceptions, but handled differently?
Yes, unlike monad, the error in algebraic effect is propagated by the compiler/interpretor, instead of user defined. But unlike implicit effect, explicit effect (algebraic effect, throwable, etc.) makes it clear how the code can go wrong.
Although explicit error through monad or algebraic effect is more clear in general, there are special cases where explicit effect is undesirable. One such example is effect pollution: low-level effects that are unlikely to cause impure behaviors are unnecessarily propagated through the call stack. This problem can make the code more verbose and difficult to handle.
That’s only true in crappy languages that have no concept of async workflows, monads, effects systems, etc.
You don't even need to sit on your ass and wait for these data types to be added to standard libraries. There are countless libraries that support those, and even if that is somehow not an option it's trivial to roll your own.
I mean, yeah, if your language does not support error values, do not use them.
Nonsense. If adopting info of the many libraries already available is not for you, it's trivial to roll your own result type.
Even if that was somehow unexplainably not an option, even the laziest of developers can write a function to return a std::tuple or a std::pair and use structured binding.
The problem is that you need some support from the language to make it easy to deal with. Otherwise you'll get into go-style infinite if (err != null) handlers that will make your code unreadable.
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.
It would be nice to include Zig's approach in the comparison. I've only just begun learning it, but the syntax seems pretty elegant from what I've seen so far.
Upvoting not because I share author's preference, but because I'm interested in reading other people's perspectives on the topic.
The article made a few good points, but a good amount of it was conjecture. I liked the part about comparing the two functions and showing that exceptions are faster but I think a big thing he's not getting is readability. Even in the functions he showed, you can directly see that the one using std::expected has the happy path and error path directly in the function signature, whereas the exception one doesn't.
As for the "error kind" trap he was talking about, that definitely exists, but ignores the fact that you can also get this same kind of error from exceptions. I've definitely gotten exceptions that I didn't understand from Python or Java libraries, but it's not a problem with exceptions but a problem with how they're shown. If there's nothing to tell me that I should have thought of that error, it shouldn't be an expectation for a dev to have thought of it.
They both have their place.
I just recently discovered a bug in lemmy bot I wrote where the lemmy API module will raise an Exception if login fails (response status code != 200), which feels extremely out of place, as the error/status code do matter in that case.
Other times exceptions make more sense as Phillip pointed out.
It's easier faster to ask for forgiveness than permission after all.
Look at gleam and elixir. Both are functional. Both use exceptions, but both also use error values as well. There is no reason why we can't have both. These are incredibly fault tolerant systems.
I didn’t read everything, but I mostly agree with the author, especially on this point:
While you can definitely abuse exceptions, functional-style error values are not a one-size-fits-all solution.
There are time and place for both. I think exceptions are good for bigger errors. Like database connection errors. Things that shouldn’t happen without any easy backup plan. Those errors might need to be escalated as high as possible where proper action can be made (like resetting the database connection and everything relying on it).
Functional style is great for smaller stuff. Like key not found in hash maps. In many cases there might be good defaults that can be used instead.
They make valid points, and maybe it makes sense to always prefer them in their context.
I don't think exceptions always lead to better error handling and messages though. It depends on what you're handling.
A huge bin of exception is detailed and has a lot of info, but often lacks context and concise, obvious error messages. When you catch in outer code, and then have a "inaccessible resource" exception, it tells you nothing. You have to go through the stack trace and analyze which cases could be covered.
If explicit errors don't lead to good handling I don't think you can expect good exception throwing either. Both solutions need adequate design and implementation to be good.
Having a top-level (in their server context for one request or connection) that handles and discards one context while the program continues to run for others is certainly simple. Not having to propagate errors simplifies the code. But it also hides error states and possibilities across the entire stack between outer catch and deep possible throw.
In my (C#) projects I typically make conscious decisions between error states and results and exceptional exceptions where basic assumptions or programming errors exist.
Does the performance cost of error checking/result types they discovered in C++ apply to languages that have native result and option types like Rust?
I would hope they were able to find efficient, performant implementations, and that branch prediction picks the expected non-error branch in most cases.
I did eventually yes. Thanks for asking. I was exhausted yesterday, and upon reading my comment again, I get the downvotes. Being a second language doesn't fully explain the wrong tone there. The article was a lot more insightful and in depth than I had mistakenly assumed.
After reading it tho, it seemed a lot more focused on performance than I think would be warranted. But that could be due to different concerns and constraints than where I'm used to working. I'd focus more on the mechanisms that best expresses the intent, and although they do discuss this well, the Venn diagram for the appropriate use of exceptions and error codes don't overlap as much in my world.
And, it's not like I'm arguing that they are wrong. It's an opinion on a choice for a tradeoff that I only think, while allowing the possibility of being wrong, might miss the the mark. Stack unwinding is by its nature less explicit for the state it leaves behind. So it shouldn't be a question of either error codes or exceptions, but which are most appropriate to express what, and when.
Even for Rust, where monads are preferred and part of the language to express and handle error codes, I would say that the statement of "newer languages like Rust don’t allow the use of exceptions", seems incorrect to me. Something like panic!("foo"); coupled with panic::catch_unwind(|| { ... } }); I believe would unwind the stack similar to that of a throw/catch.
Anyways. Thanks for reminding me to actually read the post. It was well worth it, and very insightful.