Live data from Hacker News

Away from Exceptions: Errors as Values

humanlytyped.hashnode.dev

41–50 of 145 posts

Re: Away from Exceptions: Errors as Values

#41
post #39
post #36

Earlier quoted context omitted.

> I wish that programming languages supported some contract-like mechanism of declaring: "This method can throw only X, Y and Z". If the method throws anything else, a system-defined "UnexpectedException" would be thrown, encapsulating the invalid one Boy, do I have some news for you... Like about 25 years old news. Did you ever try java?

Yes. What I'm thinking of are _not_ checked exceptions, at least not at compile-time.

Can you elaborate then?

Re: Away from Exceptions: Errors as Values

#42

It looks like GO developers never heard about "not repeat yourself", because after almost each call in GO, you write boilerplate error checking code... So you end up writing at least 2 times more lines of code.

It was a very conscious decision by go devs. To enforce local error fixes instead of the usual wrap and throw the exception again.

Re: Away from Exceptions: Errors as Values

#43

> Programming with exceptions is difficult and inelegant. Learn how to handle errors better by representing them as values. Funny how exception were invented because handling errors as values was considered to be tedious. And now, more and more languages are going backward.

[deleted]

Re: Away from Exceptions: Errors as Values

#44
post #31

> Some errors are unexpected and should stop the program; you want to use exceptions for those. Precisely the opposite: exceptions are a fail-fast mechanism that gives you an alternative to terminating the program. Now, as slx26 mentioned, it's only half of the story. Most APIs (including .NET) document exceptions badly, they're not discoverable, and if you try to use them to _recover_ from a condition, you're in for…

"> Some errors are unexpected and should stop the program; you want to use exceptions for those.

Precisely the opposite: exceptions are a fail-fast mechanism that gives you an alternative to terminating the program."

I don't see how this logic flows.

Exceptions do give you the alternative to recover, sure, but the author is saying 'the kinds of errors that produce states where you should stop the program ... use exceptions'.

You're not really disagreeing it seems.

Where you might disagree, is that the author is indicating the 'recovery cases' are more suited to being straight error returns while you're hinting at exception recovery.

Re: Away from Exceptions: Errors as Values

#45
post #40
post #34

Earlier quoted context omitted.

> For example, Java's parseInt [1] throws a NumberFormatException if the string can't be parsed. IMHO this is terrible design. It's unergonomical design, but it's the _correct_ design: the method is declared to return an int, and it can't fulfill its promise: throwing an exception is the right thing to do.

It's the correct design only if we assume that the design space didn't allow for a different return type. Kotlin for example offers toIntOrNull ( https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/to-... ) as an alternative.

A null doesn't contain any information about what went wrong and unless you religiously check your objects for null values at every turn you just turned a clear stack trace into a search for waldo at the international waldo impersonators meetup.

Re: Away from Exceptions: Errors as Values

#46
post #11

Earlier quoted context omitted.

You need all those error check code if you want to recover from error -- regardless of whether the error is coming from exception or value. If you don't want to recover, just throw it down the stack that will exit, then can you do that without exception too. Just call error() function on error which will print the error and exit(-1). No need for per-line error checking in this case too.

Realistic options are not limited to "handle at every point of the call stack where an error is encountered" like Go and "end program execution as soon as an error is encountered". Most programs handle most errors by bubbling them up to some top-level event loop and presenting some variant of abort/retry/fail to users. Exceptions are tailor-made to cover this use-case.

> errors by bubbling them up to some top-level event loop and presenting some variant of abort/retry/fail to users

The pros and cons of this approach have been covered extensively in the last decade.

the points discussed were --

//

Re: Away from Exceptions: Errors as Values

#47
post #17
post #8

"Which program is easier to read?" For me it was the second. Am I the only one ?

Tbh, to me the first one is easier to read. There's less jumping. But both are terrible. It should just be a bunch of if (...) { ... } else if (...) { ... } else { ...} etc. with no mutation of variables (what are all those v += 1 for?).

Those v += 1 implement the exact specification given above:

    1. Parse an integer N from a string.
    2. If N is NaN, fail with an error. Otherwise, increment N by 1.
    3. If N is > 3, fail with an error. Otherwise, increment N by 1.
    4. If steps 1-3 failed, set N to 3.
    5. Increment N by 1.
Those are quite strange requirements, and the resulting second code looks strange too, but... it faithfully and obviously correctly represents the given specification.

Re: Away from Exceptions: Errors as Values

#48
post #30

I'm firmly in the camp that believes that exceptions are a false economy. The post links to an "Exception Smells" post that doesn't mention one of my pet peeves: exceptions as control flow. For example, Java's parseInt [1] throws a NumberFormatException if the string can't be parsed. IMHO this is terrible design. As a side note, checked exceptions are terrible design. I wrote C++ with Google's C++ dialect where excep…

> To make it "readable" a bunch of functions had to be created.

and in doing so created code that was far more self-documenting and evidently correct. the counter-example was no where as easy to reason about (imo), even for the simple example.

> You can't step through that code with a debugger.

i don't think it's fair to comment the content of the idea based on the quality of existing debuggers. in any case, i don't think it's true that you can't step through this code on a debugger (generally): VSCode & Roslyn have no issue with this sort of structure in C#.

> The error messages may be incomprehensible.

the ones in the example may be. i've worked in a large code-base using this approach before, and there was rich error information. transformations of failure states (i.e. the Error values) are easier to do with context, as opposed to your catch-block which has no knowledge of the context in which an exception was thrown.

> I much prefer Rust's or Go's version of this, which is instead imperative.

go's (err, val) "error handling" paradigm is, imo, it's worst feature. i can't talk for rust. whilst your assertion that a failure condition is impossible to reach may be true for the code you write today, it almost certainly wont be in the future..

how many error dialogues have you seen saying some variant of "unreachable state reached"? :)

Re: Away from Exceptions: Errors as Values

#49
post #29

Earlier quoted context omitted.

Have you considered using patterns that help dealing with that? like Railway Oriented Programming https://www.youtube.com/watch?v=45yk2nuRjj8 I believe that almost every thing should return Result , because almost everything can fail and compiler should scream when you do not handle those fail pathes. That makes me believe that C#'s "FirstOrDefault" for value types sucks, because you're never sure whether the "Defaul…

> meanwhile 0 may be valid value! so we aren't sure whether it is error or an actual value This function is useful when you don't really care whether it's an error or the value. Imagine you're querying the view count of an item of the user. If the user is anonymous, the select might give an empty result, but you only care about showing a number to the user. So 0 is absolutely fine in that case. If it's important to y…

Yes, I can always use First and catch Exception, which is meh.

First returning Result or something like FirstOrNull would be better, because FoN would work for ref types - classes and stuff the same way (afaik) as FoD does, but it'd make error handling for value types like ints more precise

Re: Away from Exceptions: Errors as Values

#50
post #8

"Which program is easier to read?" For me it was the second. Am I the only one ?

The first one reads like somebody just found out about functional programming and functors and tried to "improve" a straightforward bunch of if statements. I can read the second program without having read the plain English description, but I definitely would prefer to have the comment for the first one. I think even in languages like Haskell I would prefer (just sometimes) to read just a straight if-elseif-elseif-el…

I think part of the problem is that Typescript doesn't have support for error-as-value baked into the language and pervasive in the ecosystem, so adopting that style isn't as ergonomic as it would be in a language that does. The equivalent in Rust would be far more clear and concise due to the ? operator and Result-types being ubiquitous.
Post reply on HN