top | item 33488656

(no title)

jherico | 3 years ago

Go and Rust both strike me as languages made by people who HATE Java/C++ style exception handling but thought the best way to deal with it was to invent a language that constantly punches you in the face.

the fact that the most popular Go IDE actually has logic to automatically fold and hide the constant error handling boilerplate and that people seem to have no problem with that makes me feel like I'm taking crazy pills.

discuss

order

vore|3 years ago

I feel like Rust isn't too bad at it: if you want the almost C++/Java level of wrist strain just use anyhow::Error, remember to put ? after fallible calls and downcast to extract the underlying error.

C++ and Java exceptions have non-local flow and it's hard to determine what function can throw what, so if you do want to add error handling cases you would have to inspect every function to know if it throws or not. Yeah, there's noexcept and checked exceptions but one was a late addition that legacy codebases don't use and the other everyone just does throw new RuntimeException(err) anyway.

Animats|3 years ago

After several false starts, Rust mostly got this right with returning

    Result<usefulresult, Error>
from most functions, and using "?" for "If the result of this is Error, do a return of the error". There's also a thing where you can write

    foo().context("useful info about foo")?
and pass some more info with the error.

Plus, most things that work like "open" close when their scope closes, so you don't need all that "defer" stuff.

I would have liked a good error hierarchy like Python 2 had, where there was a clear distinction between program-is-broken errors and errors due to external causes. But Rust didn't get that at the beginning, and now it's too late.

typical_gopher|3 years ago

> C++ and Java exceptions have non-local flow and it's hard to determine what function can throw what, so if you do want to add error handling cases you would have to inspect every function to know if it throws or not

In general I agree with you, but Rust is already having this problem by people making liberal use of unwrap.

nyanpasu64|3 years ago

C++ exceptions and Rust Anyhow errors lack stack traces by default, whereas Java exceptions capture them by default, aiding debugging. I'm not sure if Rust spans satisfy the same requirements as stack traces; they seem more built around logging and asynchronous execution, than error handling and synchronous code.

the_gipsy|3 years ago

Rust did it well, go just didn't. Maybe because go is the new java.