News for hackers. Stuff that matters.
Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by breadwinner on Thursday July 13, 2023 @09:20PM (#36718298)
> That bug should have been caught by tests, code reviews and good communication.

There's a reason we have compile-time checks. If you think compile-time checks should be replaced with additional tests, code reviews and good communication, then you want a scripting language, not a compiled language.

You do not wrap checked exceptions in an unchecked one... unless you are a really bad Java programmer.

Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by insanitybit on Thursday July 13, 2023 @09:54PM (#36718508)
> unless you are a really bad Java programmer.

Here's the thing. You've just described the vast majority of programmers - bad. They're really fucking bad. And language constructs that lead to bad programmers doing stupid things are, unfortunately making things worse for everyone.

Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by erik_seaberg on Thursday July 13, 2023 @10:05PM (#36718592)
stream.filter(p) requires wrapping checked exceptions, or altering the language so that they’re no longer checked.
Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by chii on Thursday July 13, 2023 @11:27PM (#36719139)
or you start doing monads instead, where p operates on a monad (aka, the Optional type).

Unfortunately, this ends up propagating out and your entire code base needs to also do this. Tho i reckon it will make your code better in the future, for a short term/temporary pain converting/adding breaking changes etc.

Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by erik_seaberg on Saturday July 15, 2023 @12:10AM (#36733343)
Yeah, switching to Scala or Kotlin would be the shortest path to do that.
Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by masklinn on Friday July 14, 2023 @12:28AM (#36719481)
You could also alter the langage woth for exceptions transparency (swift has something like that with “rethrows“).

I don’t think that’s close to sufficient to making checked exceptions work tho, let alone good.

Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by pestatije on Thursday July 13, 2023 @10:06PM (#36718596)
i do...if i ever get an IOException all i have to do is end the app, no need to deal with it in any way different than an unchecked exception

https://phauer.com/2015/checked-exceptions-are-evil/

Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by breadwinner on Thursday July 13, 2023 @11:17PM (#36719081)
Check the root message of this thread... if you use unchecked exceptions you either crash or you swallow all exceptions. Both are evil, and checked exceptions are the solution.
Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by colanderman on Thursday July 13, 2023 @11:43PM (#36719241)
That's only possible if your code is 100% correct.

Sometimes, your logic is flawed. A condition occurs which you erroneously deduced not to be possible.

Unchecked exceptions are the necessary manifestation of these unforeseen errors. Catching them is pointless, what will you do with them? Dynamically fix the logic of your program? What can be the reasonable response to "index out of bounds", try a different index? [1]

Depending on the domain it may be appropriate to convert them indiscriminately to checked exceptions at module boundaries (e.g. requests within a server) -- but within said module it remains pointless to catch them. (This is a form of swallowing.)

In other domains, crashing is the correct behavior. The code cannot proceed correctly, it must abort.

Checked exceptions are appropriate only when the exception can be anticipated and thus planned for, ideally by code closest to where it is thrown.

[1] Actually there was a paper a long time ago that monkey-patching C code to return fake "null" data in response to out-of-bounds memory accesses actually resulted in the intended (i.e. correct) behavior in the majority of cases. But I digress.

Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by breadwinner on Friday July 14, 2023 @11:54AM (#36725774)
Some errors are recoverable, some are not. When recoverable errors are possible you want to know what those errors are. If you don't then you will crash when you could have recovered. And that's the reason for writing down the list of possible exceptions where recovery and retry are possible (aka checked exceptions).

When recovery should not be attempted (example: "index out of bounds") then you don't want to declare or catch the exception, and that's when you use a subclass of RuntimeException in Java.

Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by colanderman on Thursday July 13, 2023 @11:53PM (#36719296)
A better example for IOException is network errors. If your socket dies, your code should attempt to reopen it! It should report to the user if it can't!

Moreover, it's good that code nearest the site where the exception is thrown handles the error, as only it has context for what's going on at the time. Code further up the stack won't have any clue what this random IOException might relate to.

If you're confident the IOExceptions can't occur under normal conditions -- say, you know the file has correct permissions, isn't being concurrently modified, etc. -- then, encode this belief by catching the IOException near to its origin and rethrowing as an unchecked exception.

This same pattern shows up even in languages without exceptions. In C -- always check errno; don't try to catch SIGSEGV or SIGABRT; raise SIGABRT if errno is something you don't plan to handle. In F# -- you're forced to match Ok/Error; don't try to catch exceptions; raise an exception if you don't expect Error.

Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by rrobukef on Friday July 14, 2023 @12:54PM (#36726555)
But with abstractions you can have many more functions not handling the error before you get to the actual context.

Is it worth it to thread the IOException through the network layer, then the HTTP-client library, then then business serialisation up to the actual context?

I don't want a dozen checked exceptions. (and with this approach socket-limitations and connection loss should get a different exception.) I also don't want catch and rethrow. Unchecked+checked+docs+crashes are a measured approach.

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.

Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by hinkley on Friday July 14, 2023 @02:00AM (#36720001)
I've given a lot of time and benefit of the doubt to 'testing replaces static analysis' and not only do I know this is not true, but I'm also pretty sure that testing is fundamentally broken. It can't fix anything because it can't even fix itself.

Maybe a new testing paradigm will fix it, I certainly keep an eye out for them, but nothing so far. Property based testing is better, but mostly it just reminds me that we had Design By Contract 30 years ago.

Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by sobellian on Friday July 14, 2023 @02:33AM (#36720199)
If this were 100% true, everyone would want a language for formal verification (Idris, Coq, etc). Short of those lofty heights we can quibble over the varying strictness of Haskell vs Java vs C and so on.
Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by edejong on Friday July 14, 2023 @03:14AM (#36720483)
A good programmer knows the limits of the type system. They lean on that knowledge to determine which tests to write.
Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by smrtinsert on Friday July 14, 2023 @10:17AM (#36724363)
> You do not wrap checked exceptions in an unchecked one... unless you are a really bad Java programmer.

That's not a given. It's perfectly fine to handle a checked exception via some custom runtime. The nice part is your your code base was given a chance to handle the key exceptions of this particular api all from your ide without you having to refer to documentation etc.