logoalt Hacker News

spullara • today at 6:22 PM • 5 replies • view on HN

always has amazed me that there was so much hate for java checked exceptions where handling errors ought to be the norm.


Replies

sieve • today at 7:00 PM

In the vast majority of programs, errors and non-results are irrelevant at the call site. You want to program for the happy path and not wrap every single call with error checking code or `try/catch/finally`.

    h = file_open(path)
    data = file_read(h)
    file_close(h)
    return data
is the code you want to read. This essentially is:

    return file_open(path).success(h -> h, file_read(h)).success(h, data -> file_close(h); data)
A language with thoughtful error handling primitives will support such code.
➕ show 1 reply
groestl • today at 6:35 PM

The question is _where_ the error needs to be handled. It's almost never directly at the call site. And then there's a few issues with checked exceptions and Java language things (RMI, closures, etc) which make checked exceptions cumbersome. An exception (checked or not) might be worse than sum types, but they're still a thousand times better then returning an error in-band.

➕ show 2 replies
Terr_ • today at 6:56 PM

Amen, people saw the symptoms of "needs more syntactic sugar" and misinterpreted it into "error handling is bad".

The same way that layered code should wrap/translate return types, it should also be wrapping/translating exceptions. They're both results of computation.

Past rant/discussion: https://news.ycombinator.com/item?id=42946597

kaoD • today at 6:29 PM

I agree but I think was also because Java didn't have sum types (or still doesn't? years I haven't read Java) and rethrowing is super easy, while Rust kinda nudges you into the right direction (if you need to rethrow at all, think if you want to rethrow verbatim or wrap with a semantic error type).

Right idea at the wrong time.

javier2 • today at 6:36 PM

They were overused, but some actually make sense, such as the IOException and SQLException