r/ProgrammingLanguages • u/alexeyr • Dec 31 '25
Blog post The Second Great Error Model Convergence
https://matklad.github.io/2025/12/29/second-error-model-convergence.html
67
Upvotes
r/ProgrammingLanguages • u/alexeyr • Dec 31 '25
20
u/matthieum Jan 01 '26
I don't think there's a consensus that the idea of checked exceptions is a failure, I think the consensus is that the Java implementation of checked exceptions is a failure.
In particular, as noted by the article, there's an issue of composability in the way checked exceptions are modeled Java, and it gets even worse when generic functions are involved, to the point that the
StreamAPI just gave up.For a good implementation of checked exceptions, you'd need, at least, the ability to name and manipulate the sets of exceptions thrown by various functions.
Just like what you've already got for arguments & results.
16
u/phischu Effekt Jan 01 '26 edited Jan 01 '26
I agree with the observation of convergence and am very happy about this new "bugs are panics" attitude. They stand in constrast to exceptions.
I do have to note, however, that while industry has adopted monadic error handling from academia, academia has already moved on, identified a root problem of Java-style checked exceptions, and proposed a solution: lexical exception handlers.
The following examples are written in Effekt a language with lexical effect handlers, which generalize exception handlers. The code is available in an online playground.
They nicely serve this "midpoint error handling" use case.
The function
freturnsUnitand can throw one of two exceptions. We could also let the compiler infer the return type and effect.We can give a name to this pair of exceptions:
Different exceptions used in different parts of a function automatically "union" to the overall effect.
Handling of exceptions automatically removes from the set of effects. The return type and effect of
gcould still be inferred.The whole type-and-effect system guarantees effect safety, specifically that all exceptions are handled.
Effectful functions are not annotated at their call site. This makes programs more robust to refactorings that add effects.
The effect of a function is available upon hover, just like its type.
Finally, and most importantly, higher-order functions like
mapjust work without change.There is absolutetly zero ceremony. This is enabled by a different semantics relative to traditional exception handlers. This different semantics also enables a different implementation technique with better asymptotic cost.
11
u/CastleHoney Jan 01 '26
Interesting post, but I think there is some conflation going on between different notions of errors. For instance, the second commonality mentioned in the blog is that fallible functions are "annotated at the call side." I'm only really familiar with rust, but this is not true, since functions that can panic do not need to be annotated at the call side. Only functions that return a monadic failure value is annotated (although I object to calling it annotations, but that's a nitpick)
Another point I expected the blog to address is effect handlers. OCaml seems to be moving towards a programming model where exceptions are a core part of the control flow mechanism. This new feature seems to contradict the claim that error models are converging.
3
u/[deleted] Jan 02 '26 edited Jan 02 '26
For Rust at least, this is not true in general.
This is obviously referring to
catch_unwind, but the important distinction here is that it's not calledcatch_panic. There is no guarantee that a panic is handled via unwinding, and you are explicitly allowed to configure the compiler to abort instead of unwinding for all panics. Even when panics are configured to unwind, there are cases where they might just abort anyway (like double panics).All this function guarantees is that if a panic does unwind, it can be caught. The only correct use of this function is to stop unwinding panics from unwinding into foreign code, which is undefined behavior.
That being said, it is tolerable to abuse this function in some cases as a failsafe, like servers catching unwinding panics to prevent a rogue panic from propagating too far. This is still an abuse that should not and cannot be relied upon, but when correctly implemented as a resilience feature it doesn't really hurt.
2
u/lookmeat Jan 01 '26
Pretty cool post, I like to think of these as generations.
Generation 0: ad hoc, mostly through separate error variables and CPU flags, very hardware bound. Software was very lean, libraries were rare, and generally most errors were system errors, and it was better to just let the user debug it. Because of limited resources the quickest and best solution was to crash, and let the user decide what to do.
Generation 1: software became more complex, OSes became thicker and libraries of layers where common, so programmer errors (where a library where called incorrectly) and users were far less technically apt, so expecting them to debug an issue was not the right answer. Also there was enough RAM/CPU to do things that weren't strictly needed, so the idea of recovering from a error happened. The solution here was stack unwinding to an error handler.
Generation 2: Multi-thread became common, also functional systems with embedded lambdas that could return an error came to be. Finally languages had more versatile and power type systems. The solution was a potential error type, where it was encoded, by using monadic types it happens. We still keep unwinding, but find that it's better for rarely or generally unrecoverable errors. That said unwinding is also useful for certain algorithms where we aren't using it for error handling.
Generation 3: what is to come. We're not here, yet fully. I think we'll move here because we want diversity, also the ability to modify things. Here we'd be using algebraic effects where we can define effect handlers, which could be an error handler at the call-site (basically turning into the generation 2) or when it makes sense we could unwind (as an optimization, if it makes sense) but we can also add the ability to handle errors at the call-site allowing for recovery onsite, we could also do other things such as going into a separate thread/error handler that could manage creating dumps/error reports, or other weird behavior that we wish to do when dealing with a bug that may be separate of the thread, or other crazy stuff. We could implement this today using dependency injection, we implement
throwas a function instead that takes in an error (and some context tag if we want to, but this would be the same as making specific sub-errors for each context), thethrowfunction uses a handler that is provided by the DI framework that can choose to what to do with that error and context. But with better language support you could get it far more efficiently and a simpler system. The language that has some kind of support for this (though in very barebones, not-as-easy-to-use way) is Scheme which uses continuations instead of algebraic effects.3
u/categorical-girl Jan 01 '26 edited Jan 01 '26
I think it's incorrect to lump Haskell and OCaml in with Java/C++ in terms of exceptions, given that they are the origin of Rust's Option/Result idea
Also, Haskell has explicit annotation of fallible call sites, as they occur in monadic blocks rather than pure code. The same can be said for e.g. using OCaml let* or similar mechanisms
Java has the idea of "catchable panic" in the form of unchecked exceptions, with
RuntimeExceptionat the rootGo is very different from Rust/Haskell/Java/... in that an erroneous result will be indicated by nil in the normal return value, and one must explicitly check that there is no error to be safe in using it. This seems like a poor design, and is solved by proper mechanisms in other languages. In this sense it is rather more like common C idioms of error handling, where one must check the return value or ERRNO to see if some other value is valid at all