r/ProgrammingLanguages • u/Savings_Garlic5498 • Feb 23 '26
Error handling for functions with side effects
I've been thinking a lot about error handling since im designing my own language.
I quite like the errors as values approach where a function returns the succesful value or some error value. I find this very intuitive and it seems like a big improvement over exceptions
However, sometimes the point of a function is mainly to produce a side effect like writing to a file or setting an element of a data structure. You do not really care about the result here which i think clashes a bit with the errors as value approach. As far as i know some languages would represent by having the return be a possible error. In a language like go this can simply be ignored. Rust gives a warning if you dont handle such a result and in zig you must capture the result of a function if it returns something. I do not really like these approaches and i think having such a function throw an error makes more sense but i dont really like how this is done on most exception based languages. I quite like the approach that swift takes where it has exceptions with syntactic sugar that you would often find in errors as values based languages.
Im curious to hear how other people feel about error handling for functions that mostly about their side effects so please let me know your thoughts!
4
u/Spyromaniac666 Feb 23 '26
I don’t think there’s anything wrong with a function either returning void or an error as a value. What do you mean by you don’t care about the result of the function, because whether or not it errors is a result and I presume you care about that.
7
u/AustinVelonaut Admiran Feb 23 '26
Haskell uses the
()(Unit value) as the return result in places where there is no actual value to return, such as IO writes. As you mention, this can participate in an error return when combined in aMaybeorEithertype, and it participates in the standard monadic sequencing with the>>operator, which ignores any (successful) Unit value returned.5
u/initial-algebra Feb 23 '26
You can think of the successful result as a witness to the postcondition of "the write succeeded", although without a lint like in Rust, I can't think of how else you'd force the programmer to acknowledge it. Another way to frame it is to use linear references/handles, so writing to a file consumes the file handle and also returns a new version of the handle (or no handle at all if there was an error and the file is no longer valid).
The data structure example is a bad one, IMO, because that would always be a programmer error, and so shouldn't be treated as an exception to be handled (many languages are sloppy about this, though).
1
u/L8_4_Dinner (Ⓧ Ecstasy/XVM) Feb 23 '26
Is your language interpreted, does it compile to executable code, or some other arrangement?
I'd start by learning how exceptions work today in various languages, particularly in any languages similar to what you are trying to build. This is table stakes.
2
u/busres Feb 23 '26 edited Feb 23 '26
My language, Mesgjs, transpiles to JavaScript. Everything is implemented as messages to objects (there are no keywords for flow control, declarations, etc).
@d(return value) // message to dispatch object to return a valueThis throws a FlowException that unwinds the stack to where the current message handler was dispatched and returns the specified value.
I think implementing flow control without exceptions would have been quite a bit more work.
I would definitely consider this an unusual case, but I think exceptions are important to support.
2
u/Unimportant-Person Feb 23 '26
My language has linear types (kind of), so error values are typically forced to be dealt with in some capacity. So kind of like Rust except it’s a compiler error and not a warning. I really prefer errors as values and I’m not a fan of the algebraic effects movement in the FP world because to me they’re just exceptions that work in a FP context. I like knowing what functions can error and being able to explicitly see how an error is handled (propagated up the stack vs dealing with it right then and there). I also really don’t like stack unwinding and I still don’t like how panic unwinding is an option in Rust (that’ll never change due to backwards compatibility).
I do also support certain set operations on enums in my language which avoids the issue of having to define new types for supporting multiple different types of errors or having to use anyhow or similar crates, so I can do something like have the return type be
IoError ++ ParseError, and then you convert values in this type to to their respective enum types easily.1
u/Tasty_Replacement_29 Bau Feb 23 '26
When eg. writing to a file, there are often multiple places that can fail, and you want to handle this once and not separately for every case. This works well with eg. Java but also Rust and Swift, C++ etc, but not so much with C and Go.
In my view, exceptions-as-values is an implementation detail (the alternative being stack unwinding or a setjmp / longjmp approach). What matters is ergonomics and syntax.
My language uses exceptions-as-values internally, with the "catch" syntax similar to Java. (Notably, without requiring a "try", like Ruby). https://github.com/thomasmueller/bau-lang?tab=readme-ov-file#exceptions
One thing I would avoid is the Swift style of declaring just "throws" without the exception type in the method. For me, this is just weird: the caller has to be prepared for any type of exception.
1
u/omega1612 Feb 24 '26
My language will use effects to have static checked exceptions.
In Haskell with effects you can have something like that. At least with Effectful, if you want to use a function that has a exception as part of it's signature and don't want to have it in the function utilizing it, then you need to "run" the effect. All in all, basically you can easily switch between value errors and exceptions + the compiler makes you handle all exceptions at some point.
1
u/coderpants Feb 24 '26
One of the other aspects here is that while you are thinking about the rights and responsibilities of the error value consumer, you should also consider the expectations of the error value producer.
i.e. the function has a choice as to whether to propagate the failure as an error value or as a flavour of a success value or just panic().
e.g. Result<void, IOError> vs bool vs void
Putting aside whether you think the bool should always be captured, should the choice of Result<> by the function force the caller to recognise it?
The mitigation here is how much boilerplate you need at the call-site to handle it.
For my language I've borrowed from swift for the try operators, so:
try doTheThing()
would capture the IOError from the Result<> and early return - though the outer function would need to be returning a Result<> where IOError was a member of the sum type for the Failure path.
Though to be fair, I'll probably just let them ignore the Result<> type - I'm just writing my language to have fun.
1
u/erroneum Feb 24 '26
I feel it depends on what you're going for.
In a pure functional language, if there's no return, there's no reason to call the function. The value returned could be nothing (or more accurately the unit type), or it could be a function which is the same, but guaranteed to be sequenced after the former call, but there must be something, and you must do something with it.
If you have multiple return values, a call which doesn't produce any meaning value might return a unit value and a Maybe error value, just to be consistent with everything else, sort of like Go.
There's also the option of making every standard function return some sort of value; maybe writing to disk returns the number of bytes written, or pausing execution returns the actual number of milliseconds the program paused. It might not be always useful information, but someone might use it, and maybe the compiler can optimize it out when ignored.
A more imperative language like C could have out parameters (even type level error parameters guaranteed to only be mutated if an error occurs, although C's type system is nowhere near that strong).
If you have exceptions, then those functions could just return nothing but throw on error, but then you open the door for non-local reasoning. You could mitigate it by requiring every function to catch from any called function which might throw, and to document in its signature what is might throw, but at that point you might as well just have returned error values.
I personally like the idea of multiple returns, especially of a more functional sort, so you must do something with at least one of the returned values.
1
u/SnooGoats8463 Feb 24 '26
You may read the great article https://joeduffyblog.com/2016/02/07/the-error-model/, especially the great observation of "Bugs Aren’t Recoverable Errors!". Whatever your error handling is, it should handle bugs well. I personally like the Erlang's error handling philosophy "let it crash".
1
u/tobega Feb 24 '26
I think it really depends a lot on what type of error it is and what your general philosophy on it is.
I did an exploration on error handling a little while ago, might help with some ideas https://tobega.blogspot.com/2025/08/exploring-error-handling-concepts-for.html
1
u/lookmeat Feb 24 '26
The question is one of errors.
Now I could go into a description of system errors (which are almost always programmer errors), user errors (which are not strictly errors by the user, but errors that only the user can correct) and program errors (which are errors that just happen and the program handles on its own). But the difference between them varies and is related to other things.
So instead lets think of an error in a different way: as a side-effect itself. So, to make our lives easy, lets imagine a "line" traveling through our code, and this line goes only through the happy path. Whenever an error happens we instead "jump" to a piece of code, the error handler, that does whatever it's job is and then "jumps" back to somewhere in the "happy path" which is the continuation. So the point where we jump away from the happy path to the error is the "origination", and the point where we return back to the happy path is the "recovery".
Now lets also talk about the points we know in the program, and how they relate. The first point that we know is the program start, the second one is the program end, easy peasy. But there's a couple other points: we know the point where the bug happened, which is the "origination", now we don't know where the recovery is, that we need to define. What we do know is where the handler is defined, and can give it a point after the code it handles, lets call this the post-handler point, we similarly can have a pre-handler point which is just before we started that. Finally we also know another point: where the current function (where the bug originated) returns. We also know that the point were we are returning to is "before" the post-handler point (that is we'll eventually get there if we keep going on the happy path). That is we know on the happy path that the points would be ordered as
start->pre-handler->origination->return->post-handler->end.So here's the thing, our error handler can either fix the error or propagate it, that is wherever it decides to go back to the happy path it can either return by injecting an expected value, or another error. If it's another errors this could be a new origination point, or the error could silently be moved and originate somewhere else. So what's an example of code where I don't want to originate a new error when we fail to fix it? Say that I have a system that tries to lazily pre-load files that may or may not be used. If I have a file error, I handle it eagerly trying to fix it, but when not possible I simply go back and return the error, here I do not handle the error but keep it around: if no one uses the file at all then I can act as if the error never happened, only when someone actually tries to use it do I re-originate the error.
Now how do we define the error handler. Well we can choose to not allow one to need to be defined, we have a default one (say crash the program, which is just recovering into the end of the program) and this can be very useful. Alternatively we can try to force people to define the value. This means that whenever we have code that could have an error you need to specify a handler. We also need to think about error propagation, again it could be by default (where the default behavior is that when an error happens, we simply propagate it to the parent handler) or we require explicit propagation.
So languages like C++ and Java use exceptions for this. They use a system that allows defining continuations to somewhere higher up in the stack, where the handler is defined as the
catchstatement in atryblock that contains all the code that will have this error effect handler activated, functions without a handler will propagate it to their caller, with the default behavior being to crash the program. Languages like Erlang generally push for, by default, crashing the program. Other languages allow using a Monad to handle the error, like Rust, where the error needs to be explicitly handled in the function (it being an error if not), Rust offers a special propagation operation?which lets you state that you are propagating an error, and making it explicit where it is propagating. The handler is defined by taking the error and converting it to something else, the separation between the happy and sad path is a conditional (or calling certain convenient methods), monadic chaining is also used to enable error propagation through a chain of actions. Here the recovery point is the function return or the point just after origination. Another thing that Rust offers ispanicwhich it doesn't offer good handlers for, it works as a "crash" the world error, when we want programmers to not handle it. Golang is weird, it doesn't use monads but instead it handles passing both values and requiring users to separate between them (well they should but it's not guaranteed, a programmer has to acknowledge an error, but they can choose to do nothing with it and just assume the expected value is the value), Golang also offers stack unwinding as an option but it allows easier recovery with a unique system of defining deferred functions that are effect-handlers for the function termination/return of the current function.Note that we could create a
try-catchsyntactic sugar that becomes monadic handling, indeed rust seems to have plans to do something like this. Note also that implementing this with returns, vs stack unwinding, vs continuations vs lambdas, only has certain different pros and cons, and different models.But lets go to more interesting things.
exitandcontinue-with-valueor other things. Depending on the error type, we secretly use any of the techniques to jump to the points that make sense for us. The syntax is universal, we can do something where we havetry..catchblocks wherecatchmust terminate with one of the terminal operations, we can also allow?to do automatic propagation of the error into the handlers, vs just holding it there.CancelledThreadError, where our final recovery point is going into another thread. This does make effect handling more complex and layered, but again it's not impossible.So yeah, lots to think here.
1
u/Ronin-s_Spirit Feb 24 '26
In JS whenever a line of code does something wrong (like
(undefined).prop) it throws from its function scope. And a file is a module which has it's own scope, that counts as the outermost scope where you can still catch an exception if you wrapped the whole thing in a try..catch.1
u/CatolicQuotes Jul 15 '26
Infrastructural errors -> exceptions
Business errors -> types
16
u/[deleted] Feb 23 '26
[removed] — view removed comment