top | item 45862976

(no title)

mabster | 3 months ago

I'm the opposite - I really like checked exceptions in Java because it's very easy to see how developers are handling errors and they also form part of the function signature.

Most functions will just pass on exceptions verbatim so it's better than error return values because with them the entire codebase has to be littered with error handling, compared to fewer try catch blocks.

setjmp, etc. are like unchecked exceptions, so I'm also not a fan, but I use this occasionally in C anyway.

discuss

order

fauigerzigerk|3 months ago

>I really like checked exceptions in Java because it's very easy to see how developers are handling errors and they also form part of the function signature.

Errors as return values also form part of the function signature in many languages.

>Most functions will just pass on exceptions verbatim so it's better than error return values because with them the entire codebase has to be littered with error handling, compared to fewer try catch blocks.

The question is whether you think that calls that might pass an error up the call chain should be marked as such. I think they should be.

I wouldn't call this "littered with error handling" just because a certain language has decided to do this in a way that resembles industrial style fly-tipping rather than just littering.

phanimahesh|3 months ago

Why would errors as return values have to propagate any farther in the codebase compared to errors as exceptions? If exceptions can be handled, so can the value based errors.

tialaramex|3 months ago

The criticism as I understand it isn't about where the errors are actually handled but the ceremony needed in an errors-as-values model, most obviously in Go where you've got to write a couple of lines of test and early return explicitly for each such possible error, compared to C++ where you write nothing.

Rust's Try operator is the minimal ceremony, a single question mark acknowledges that we might not succeed and if so we return early - but there was still ceremony for each such early return.

I happen to think exceptions are inherently a bad idea, but for a different reason.