News for hackers. Stuff that matters.
Re:Views on Error Handling by enriquto on Saturday July 18, 2020 @05:35PM (#23885047)
The very concept of "error handling" is absurd.

There are no errors, just unnecessary abstractions and control flow hacks. You try to open a file; either you can or you cannot, and both possibilities are equally likely and must be handled by normal control flow in your program. Forcing an artificial asymmetry in the treatment of both cases (as championed by the error handling people) adds ugly complexity to any language that tries to do so.

Re:Views on Error Handling by gary_0 on Saturday July 18, 2020 @06:19PM (#23885329)
The problem of error handling arises due to type systems and function call semantics.

  data = open("/var/foo")
Most programmers are going to expect 'data', the result returned by 'open', to be a type that allows reading/writing the file. The programmer expects /var/foo to exist, or they might have checked before calling 'open', but even that's not foolproof.

Historically, a failure might just set 'data' to an invalid value (like 0 or null) but that ended up being a bad idea. And we needed some way to return more information about the error. So we started doing this:

  error = open(data, "/var/foo")
But this mainly just complicated things. Is 'data' input or output? The function doesn't return its actual output. And it's still possible to ignore 'error', so 'data' is still potentially undefined.

Then exceptions were invented so we could use proper function call styles again, and the program wouldn't go into an undefined state. Instead, the error could be handled with separate logic, or the program would halt if it was ignored. This was far from a perfect solution, though.

Then sum types entered the mainstream, so 'data' had well-defined ways of returning something other than the expected result. But that resulted in a lot of competing conventions and stylistic decisions for what to do when 'data' is an error type, that haven't quite been settled yet.

Re:Views on Error Handling by jjjensen90 on Saturday July 18, 2020 @08:24PM (#23885961)
Again referring to Go's imperfect but interesting handling of this problem, the style in Go would be:

  data, err := open("/var/foo")
  if err != nil....
In fact, the compiler will make you deal with both values after assignment unless you explicitly ignore one of the return values.
Re:Views on Error Handling by TeMPOraL on Sunday July 19, 2020 @06:15AM (#23888326)
This way a combinatorial explosion lies.

Each library/system call you do can result in a set of possible consequences. We usually don't care about them equally, though: in fact, in the file example, 99% of the time we care about whether the file was opened or not - and in the latter case, we don't need to know why. So the asymmetry is already introduced by the intent of the program - there's usually only one path of execution we want; other ones are distractions. Error handling exists to express that asymmetry of caring at tool level.

Re:Views on Error Handling by wvenable on Saturday July 18, 2020 @07:32PM (#23885704)
I completely disagree. A program or function is designed to perform an operation. If that operation requires the contents of the file, then the program cannot continue unless it successfully reads the contents of the file. There is already a natural asymmetry. If you cannot open the file, there isn't any more to do.
Re:Views on Error Handling by Mikhail_Edoshin on Sunday July 19, 2020 @04:18AM (#23887871)
An "operation" is not something inherent to the code. If we look at a function that may get what we call an error, we'll see that in either case it completes and returns control to the caller. We label one such result 'success' and another 'failure' because we also have an idea of purpose of the function, but the purpose does not exist at the code level. Maybe this is why we struggle with errors.
Re:Views on Error Handling by wvenable on Sunday July 19, 2020 @02:22PM (#23891259)
Once you give a function a name, it has an operation that's inherent to it.

If you have a function called `Add` that takes 2 parameters and returns the sum of those two numbers. That's the operation. If the web service that you call to perform the sum is down, it cannot return that sum, so that's an failure.

The code that calls this function needs that sum to continue it's operation. If it cannot get that result it cannot produce it's own result and that error needs to be propagated. Maybe the entire program has a purpose that it cannot now be completed and should be aborted.

Re:Views on Error Handling by Mikhail_Edoshin on Tuesday July 21, 2020 @03:40AM (#23905050)
If you give a name to a mountain, it doesn't affect the mountain, it affects you :)

Joking aside, consider the following case. A function needs to print a series of reports and opens Print Setup. The user changes his mind and presses "Cancel". Now the function cannot continue and needs to stop the planned operations, undo what it has done so far, and return control to the user.

The internal mechanism you're going to use for this will be most likely the same mechanism you use to handle errors. But what you're handling is not an error, at least in the common usage of the word, because nothing erroneous is happening! Everything goes exactly as should and according to the general purpose of the application.

My point is that the current dichotomy of success and failure does not help us to solve the seemingly simple problem of errors.

Re:Views on Error Handling by wvenable on Wednesday July 22, 2020 @12:24PM (#23918669)
> The internal mechanism you're going to use for this will be most likely the same mechanism you use to handle errors.

Which is exactly how exceptions work. If you're using RAII or similar pattern in your language there is no separate path for cleaning up errors than from the normal cleanup. That's the point actually. If the user presses cancel or an exception is raised the eventual stack unwinding will undo everything.

If you have a bunch of conditional statements for every possible error, you're actually creating more situations that are unique for errors. You have all these paths to test for. With exceptions there is only the happy path both in the operation and the cleanup.

Re:Views on Error Handling by uhoh-itsmaciek on Sunday July 19, 2020 @04:47AM (#23887969)
That's a neat observation. If you're writing a function as "How do I get from A to B?", that's more error-prone than "What are all the possible outcomes of trying to get from A to B?"
Re:Views on Error Handling by wwright on Sunday July 19, 2020 @12:52PM (#23890585)
I disagree; programming languages and code are built explicitly and exclusively to perform a function. Operations and purpose are more inherent to the code than their mathematical/logical nature.
Re:Views on Error Handling by Mikhail_Edoshin on Monday July 20, 2020 @01:48AM (#23894986)
Yes, everything is built for a purpose. But the raw materials used to make a thing do not change because of the purpose. They follow their own laws, it's us who ascribe the meaning to the resulting combination. Laws are something we cannot change, only use. But purpose is a concept. We do need concepts to create and operate things, but if we shouldn't mistake them for laws. Concepts are something much more pliable :)

It's all rather abstract, I guess, but yesterday I found an example while playing a game, "Opus Magnum". You are to solve puzzles by arranging mechanical arms to move marbles. Initially I mentally referred to actions of the arms as "taking a marble" and "placing a marble" and solved quite a few puzzles this way. In all solutions all my arms did exactly that: they took and placed marbles without doing any purposeless movements.

Yet yesterday the next puzzle looked too tiresome to built. I had an eureka moment once I realized that the arms do not take and place marbles, they merely close and open their grips and if there's a marble, it gets caught, but the arms themselves have no idea whether they're moving a marble or not. And once I allowed myself to let them do "purposeless" movements without marbles, I found a simple and elegant solution :)

Re:Views on Error Handling by enriquto on Sunday July 19, 2020 @05:39AM (#23888173)
> I completely disagree.

Disagreeing is alright, but here you don't really do, do you? I can translate the paragraph you have written into pseudocode:

> If that operation requires the contents of the file, then the program cannot continue unless it successfully reads the contents of the file. (...) If you cannot open the file, there isn't any more to do.

    if (file opening fails):
        stop doing things
    else:
        continue with your operations
This is just a regular "if-else" that can be done with any programming language. The behavior of your program when the file cannot be opened is part of the specification; just as its behavior when it can be opened. I agree with you on that, and I add that the desired behavior can always be implemented using regular control flow constructions. You do not need a specific language construct for "errors", as you have proven by the algorithm that you have described in your text.
Re:Views on Error Handling by wvenable on Sunday July 19, 2020 @02:02PM (#23891107)
> stop doing things

This is what we call raising an exception.

> I add that the desired behavior can always be implemented using regular control flow constructions.

I agree. But that's not a very interesting observation. We added language specific constructs for errors not for the computer but for the human. These constructs make reading and writing code easier and safer.

A code with if-else constructs for every possible error condition is really hard to follow and very brittle. If the result of every error condition is the same (stop doing things) we have developed constructs to make that path easy.

The problem with errors is that understanding and resolving them is often non-local. If a network call fails the code where that fail happens doesn't have enough information to resolve it. If the solution is re-try the entire operation 3 times and then give up, the handling of this error must happen back where the operation starts not some random place in the middle where it actually occurred. Maintaining all the if/else necessary to move that information up through potentially dozens if not hundreds of calls is extremely difficult. And, it turns out, completely unnecessary and easily automated.

Re:Views on Error Handling by renox on Sunday July 19, 2020 @06:04PM (#23892828)
Ok, then replace all additions in your program with a function returning either an error or the result. Same with logging statements. And don't ignore the errors.. Is your program still readable?
Re:Views on Error Handling by camgunz on Saturday July 18, 2020 @08:20PM (#23885938)
I agree. You don’t open a file, you try to open a file. When you get a handle back, that’s your library skipping steps.
Re:Views on Error Handling by dllthomas on Saturday July 18, 2020 @05:53PM (#23885162)
Can you name a language/ecosystem that gets it right?
Re:Views on Error Handling by dinosaurdynasty on Saturday July 18, 2020 @06:19PM (#23885330)
Rust seems pretty close to this (with Result<T, E>), though there is also the panic system.
Re:Views on Error Handling by platz on Sunday July 19, 2020 @12:01AM (#23886896)
it's trivial in any language to define and use a Result<T, E> type.
Re:Views on Error Handling by saagarjha on Sunday July 19, 2020 @03:41AM (#23887715)
No, not really. Languages that don’t really embrace such a type can usually never make its use ergonomic. Adding such a construct to C or Java would be “trivial” but its use would be exceedingly painful.
Re:Views on Error Handling by The_rationalist on Sunday July 19, 2020 @04:09AM (#23887839)
Here's an example of a result type in Kotlin implemented as a library monad. (the language is getting an official one but is currently less featureful than this lib)

You can use exception or Result depending on when it makes the most sense and this is the best of both worlds. https://github.com/kittinunf/Result

Re:Views on Error Handling by saagarjha on Sunday July 19, 2020 @04:49AM (#23887983)
Swift had a bunch for a while too, but it’s getting an official one as well. Again, that’s only because the language provides the right tools for it to exist, in languages where you don’t have this you’re going to have a very hard time trying to add these things in.
Re:Views on Error Handling by aw1621107 on Sunday July 19, 2020 @03:03AM (#23887565)
That depends a lot on what you mean by "use". Pretty much every commonly used language would be able to define something that can hold one of two things and provide ways to determine which one is present and retrieve one of the two options, but comparatively few are going to be able to provide the same guards against accidental or intentional misuse that pattern matching gets you.
Re:Views on Error Handling by jjjensen90 on Saturday July 18, 2020 @05:58PM (#23885196)
Though I don't think it's perfect, languages like Go that treat errors as simply another type and you check as a return value do get closer to treating errors and success symmetrically versus an exception throwing and typing system like some other languages.
Re:Views on Error Handling by monocasa on Saturday July 18, 2020 @06:01PM (#23885212)
Building on that, the ADT languages with full option types here are fantastic here, because the shape of the data in each case can be taught to the compiler.
Re:Views on Error Handling by vvanders on Saturday July 18, 2020 @06:25PM (#23885363)
Erlang / OTP.