News for hackers. Stuff that matters.
Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by colanderman on Friday July 14, 2023 @12:04AM (#36719354)
> You do not wrap checked exceptions in an unchecked one... unless you are a really bad Java programmer.

I disagree -- this is the correct thing to do if you believe it is not possible for the checked exception to occur. (Catching it is wrong -- what would you do to correct something which you believe not to be possible? Forcing the caller to handle it is wrong -- if you don't know what to do with it, they sure won't!) Wrapping checked as unchecked encodes your belief that should it occur, it is a logic error, akin to out-of-bounds array access or null pointer dereference.

(Of course, swallowing expected exceptions one is simply too lazy to do anything about is poor practice! Not disagreeing with that.)

Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by rixed on Friday July 14, 2023 @12:49AM (#36719587)
> if you don't know what to do with it, they sure won't!

Actually no. For instance, the caller at some point up the stack may know if something is worth retrying.

Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by colanderman on Friday July 14, 2023 @10:11AM (#36724279)
Then you should unwind your state correctly and rethrow as a checked exception stating as much. If there levels up the stack I see a random unexpected "IO Exception" from your library I have no idea whether you are still in a valid state to retry. If I instead see a "Foolib Request Aborted: reason IO Exception" I know you've written logic to handle that case, and I know that retrying is appropriate.
Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by Hermel on Friday July 14, 2023 @02:07AM (#36720042)
> I disagree -- this is the correct thing to do if you believe it is not possible for the checked exception to occur.

If it is not possible to occur, then it should not be part of the API.

The only time I rethrow a checked exception as an unchecked exception is when the code is still under construction. The default of the eclipse code generator is to log and ignore caught transaction. I think wrapping into an unchecked one is the better default behavior for incomplete code under a "fail fast" policy.

Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by colejohnson66 on Friday July 14, 2023 @09:18AM (#36723571)
> If it is not possible to occur, then it should not be part of the API.

Ah, but what if it can occur, just never with what you pass in? Suppose a function is documented to throw some checked exception if some parameter is a negative number, but you pass in a positive literal/constant? In such a situation, the checked exception will never occur! With Rust, for example, this is easily done with an `unwrap()` (and, possibly, a comment) to assert said belief, but with checked exceptions, there's no way to force the compiler to squash the required check.

Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by colanderman on Friday July 14, 2023 @10:14AM (#36724317)
Example: validation of serialized data followed by deserialization. Deserialization properly should throw a checked exception, since invalid serialized data is very much a thing. But in this case the serialized data is known to be correct (because it passed validation). The checked exception will never occur, and were it to, there's nothing we could do about it, because it reflects a logic error, same as out of bounds array access.

The algebraic data type equivalent of this shows up all the time in functional code -- unwrapping a result type you know can't be Error/None/etc. because of logic. You don't rewrap it and force the caller to deal with a case which logically cannot occur; instead you throw an unchecked exception.