News for hackers. Stuff that matters.
Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by jabiko on Thursday July 13, 2023 @06:32PM (#36717048)
Lets be honest. The more likely thing is that either the coworker would use an unchecked exception or that they would change the callsite to:

  try {
    theUpdatedFunction();
  } catch (MyNewCheckedException e) {
    logger.warn("Whoopsie doopsie", e)
    throw new SomeUncheckedException("Something failed, idk", e)
  }
Which really is a zero sum game. The code still breaks the same way, but the checked exception gets eventually wrapped in an unchecked one. We still would have the situation that someone changed the behavior of the function in a way that is incompatible with your usage.

That bug should have been caught by tests, code reviews and good communication.

Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by __jem on Thursday July 13, 2023 @06:43PM (#36717127)
Or, worse, you're reviewing a large diff, see the logger statement but don't notice that the junior whose code you're reviewing forgot to rethrow. Now you've accidentally entered uncharted territory where way more scarier things can happen to your application than just crashing. Of course this shouldn't happen, but it does.
Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by twelve40 on Friday July 14, 2023 @02:26AM (#36720163)
that can be somewhat addressed with decent unit test coverage
Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by doliveira on Thursday July 13, 2023 @07:07PM (#36717316)
If the Future, Executor, Optional, Streams, etc played nice with checked exceptions it would probably be different. Right now at some point you do have to wrap it into an unchecked exception.
Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by throwaway2037 on Friday July 14, 2023 @06:20AM (#36721906)
I agree. It is weird that for interface Runnable there isn't a ThrowingRunnable. I always create them in my projects and make my callbacks use them along with ThrowingConsumer and ThrowingProvider.
Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by phillipcarter on Thursday July 13, 2023 @07:54PM (#36717682)
Yep. This is similar to a viewpoint by Anders Hejlsberg and many on the C# team at the time: https://www.artima.com/articles/the-trouble-with-checked-exc...

> You see programmers picking up new APIs that have all these throws clauses, and then you see how convoluted their code gets, and you realize the checked exceptions aren't helping them any.

And he goes on to elaborate on how this gets more complicated when versioning is introduced into the mix.

Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by breadwinner on Thursday July 13, 2023 @09:23PM (#36718315)
Hejlsberg is a terrific compiler writer. Turbo Pascal was awesome! He is not a good language designer, however. The exception mess in C# is the proof. See my comment at the root of this thread.
Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by bheadmaster on Friday July 14, 2023 @01:45AM (#36719914)
I doubt you can evaluate man's skill through a single project. It's not nice to use ad hominem to advertise your own comment.
Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by 411111111111111 on Friday July 14, 2023 @11:45AM (#36725641)
Their comment was the one that this thread is bases on. They're merely pointing out that the original comment already gave an example why its a mess.

I disagree with their point though, in java the lib would've probably just converted the exception to a runtime exception so the API doesn't change...

Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by phillipcarter on Friday July 14, 2023 @10:17AM (#36724369)
Yes, not a good language designer responsible for (checks notes) one of the most-used and most-loved programming languages on the planet.
Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by interlocutor on Friday July 14, 2023 @11:02AM (#36724976)
Do you mean Turbo Pascal? He didn't design it, Niklaus Wirth did. Or do you mean C#? James Gosling designed most of that (C# got its start by copying 90% of Java). Do you mean TypeScript? That's mostly JavaScript.
Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by phillipcarter on Friday July 14, 2023 @11:59AM (#36725840)
You're certainly entitled to your opinions.
Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by peterashford on Sunday July 16, 2023 @04:43AM (#36744852)
Those are facts
Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by blackoil on Friday July 14, 2023 @12:58PM (#36726601)
I would say he is one of the better language designers. Unlike many language designed by theorists and academics, he understands how the engineers are using the language and how a feature is used/abused and develops around it.
Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by coldtea on Thursday July 13, 2023 @07:57PM (#36717707)
>The code still breaks the same way

That's not a valid argument. This is an explicit decision, so they reap what they sowed.

One could just as well make the same argument for Optionals, that you can just do:

  let val = foo.unwrap();
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.

Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by hinkley on Friday July 14, 2023 @01:56AM (#36719973)
Let's be really honest

  try {
    theUpdatedFunction();
  } catch (MyNewCheckedException e) {
    logger.warn("Whoopsie doopsie", e);
    throw new SomeUncheckedException(e.message);
  }
There's a special place in Hell for these people, but at least they'll get to see all of their friends again.
Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by mdaniel on Friday July 14, 2023 @10:06PM (#36732454)
Oooo, lookit you working with professionals; I've seen waaaaaaay too many instances of this shit

  try {
    somethingImportant();
  } catch (Exception e) {
    // can't happen
  }
IJ finally started chirping about this other anti-pattern

  try {
    doit();
  } catch (Exception e) {
    System.err.println("something didn't work");
    // failure to reference "e" is now a yellowbox
  }
I have to constantly ask for them to either log it, or rename the variable to `ignored` to get IJ and future readers on the same page that it was intentionally swallowed
Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by hourago on Friday July 14, 2023 @08:14AM (#36722758)
> throw new SomeUncheckedException("Something failed, idk", e)

Who does that? It looks like an anti-pattern.

Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever by bedobi on Friday July 14, 2023 @09:33AM (#36723787)
> That bug should have been caught by tests, code reviews and good communication

This is such an unproductive cop out response. The problem could be completely solved by functional error handling, which leverages the compiler to force you to handle exactly the errors than can happen, no more, no less.