News for hackers. Stuff that matters.
Re:Why Checked Exceptions Failed by necovek on Saturday July 15, 2023 @08:40AM (#36736070)
I am surprised an even more obvious interface definition of

  public interface ExampleInterface {
      void foo() throws IOException, AnotherException;
  }
was not covered: is this also not supported?

That would solve a lot of use cases if inheritance is respected.

Re:Why Checked Exceptions Failed by ailef on Saturday July 15, 2023 @08:49AM (#36736157)
This is indeed the supported way to do it.
Re:Why Checked Exceptions Failed by necovek on Saturday July 15, 2023 @01:21PM (#36739039)
Thanks for confirming as I am not familiar with Java, but good to know it does make sense. As such, I am not sure what did the original author complain about because that's perfectly sufficient language support.
Re:Why Checked Exceptions Failed by masklinn on Saturday July 15, 2023 @08:51AM (#36736178)
It is supported, but does not actually solve anything, you're still limited to whatever checked exceptions the creator of the interface has decided might be valid (it's just that in the original it's "none").

Let's say that the creator of the interface allows IOException, but the backend of my implementation is a database so I have SQLExceptions, same issue.

Is the creator of the interface supposed to add every checked exception in the standard library? Ignoring that this still isn't all of them, then the caller is hosed because they have to handle (or rethrow) every single one of them which is no better. So they probably go "fuck it" and just handle / rethrow Exception. At which point you can just do that on the interface, and it's not helping anyone, and you're better off just not saying anything.

Checked exceptions simply don't mesh with the language: because the language provides limited to no way to abstract over them they're an issue every time you're trying to be generic over anything, the only situation in which they kinda sorta work is if the entire callchain is concrete, which not only is very limiting but it's very much not idiomatic.

Re:Why Checked Exceptions Failed by maratc on Saturday July 15, 2023 @09:28AM (#36736546)
> Let's say that the creator of the interface allows IOException, but the backend of my implementation is a database so I have SQLExceptions, same issue.

The creator of the interface should decide on a generic exception type:

  public interface UniversalStorageInterface {
      void store() throws StorageException;
  }
Then, in the present, the FileStorage can define (and throw) a FileStorageException (inherits from StorageException), and the SqlStorage may define (and throw) a DatabaseStorageException (same).

In the future, where we might want to store everything on a (non-existing yet) Cerulean backend, we would then define (and throw) a CeruleanStorageException (again, an implementation of StorageException), and our basic Interface would not need to change. We would also have no need to recompile FileStorage or SqlStorage (or proprietary SteelBlue storage where we have no code).

Re:Why Checked Exceptions Failed by masklinn on Saturday July 15, 2023 @09:35AM (#36736606)
So now whoever creates the interface needs to define an exception type per method, and whoever implements the interface needs to define a subclass of that per method and catch-and-wrap every exception their callee raises.

You better add some serious tooling built into the language to facilitate this, because from experience ain't no way anyone's going to bother with this if they have to handroll it, even with IDE codegen assisting.

And it still doesn't solve the issue of generic interfaces like streams.

Re:Why Checked Exceptions Failed by maratc on Saturday July 15, 2023 @09:49AM (#36736734)
It doesn't have to be a type per method, and if someone is "going to bother with this" depends on the application — I guess in many cases it's perfectly fine to write a happy code with a single catch where you present a sad-face emoji and "try again later" line; in other types of applications this just won't fly.
Re:Why Checked Exceptions Failed by necovek on Saturday July 15, 2023 @01:15PM (#36738959)
Whether you do it with exceptions hierarchy or error types or something else, it's pretty much the same thing: you either handle all the errors or not (or anything in between).

How do you solve that error complexity with any other approach and how is that different using any other syntax sugar?

The only thing I dislike about exceptions is that they are ignored by default allowing people to all too easily write code with no error checking.

Re:Why Checked Exceptions Failed by necovek on Saturday July 15, 2023 @03:23PM (#36740087)
Note that StorageException in the previous example could also simply be a BaseStorageException that all the methods throw, even if one of them is DiskFullError(BaseStorageException) or ObjectStorageLimitExceededError(BaseStorageException), and another is InvalidFilenameError(BaseStorageException).
Re:Why Checked Exceptions Failed by throwaway290 on Saturday July 15, 2023 @09:27AM (#36736534)
Maybe your SQLException should be caller's IOException? I don't code Java but IMO callers should be hit with errors at the right abstraction level. If caller is using that interface and not your low-level backend functions directly then caller should expect IOException not SQLException.

If the interface does not provide exception types for all cases so you are stuck rethrowing your error as a wrong one, and I'd say that means the interface is bad but not checked errors are bad...

Re:Why Checked Exceptions Failed by funnymony on Saturday July 15, 2023 @10:09AM (#36736906)
“bad” might not be the right word. Maybe better “usefulness for current population of developers is very limited”.
Re:Why Checked Exceptions Failed by PretzelPirate on Saturday July 15, 2023 @08:52AM (#36736186)
Wouldn't it still be a problem? By putting "throws [Exception List]" in the interface, the interface making assumptions about the implementation details of every potential implementation.

The interface can't know every exception that an implementation might throw, and using your example, some implementations may not use IP at all and won't throw those exceptions.

It seems like you'd end up needing to list every possible exception on "foo() " when defining the interface, and then handle every possible exception in any code that uses "ExampleInterface".

At that point, it would be better to not annotate exception information at all.

Re:Why Checked Exceptions Failed by grumbel on Saturday July 15, 2023 @09:09AM (#36736356)
> the interface making assumptions about the implementation details of every potential implementation.

No, as you'd force the implementation to catch their implementation specific exceptions and rethrow them in the way defined by the interface.

That's really the crux of it, when I call foo(), I expect to see a FooException when something goes wrong, since that's the only one I can meaningful react to. If I get an ImplementationDetailException that I never heard from and that isn't specified in the interface, how am I supposed to react to that in a meaningful way?

If exception are supposed to be used for error handling, you have to actually report and handle them in a well specified manner, you can't just treat them as a slightly less crashy version of a SIGSEGV.

Re:Why Checked Exceptions Failed by masklinn on Saturday July 15, 2023 @09:31AM (#36736577)
Unless the language does that for you (both enforcing it, and providing tools to make it absolutely trivial), it will never happen, it's just way too much overhead to have to define a new exception for every method in an interface, then a sub-exception for every method in the implementation of that interface, then go through the internal conversions from the underlying exceptions to the parent one.

People can't be arsed to do that in Rust where there's ways to abstract over those things and macros to define them.

If that's the route you assert is necessary, you need something like Zig's errors.

Re:Why Checked Exceptions Failed by necovek on Saturday July 15, 2023 @01:00PM (#36738801)
What you are saying is that people can't be arsed to do proper error checking, which is absolutely true.

Making a sane hierarchy of exceptions is the smaller of the problems of actually handling them everywhere.

Re:Why Checked Exceptions Failed by exceptotron on Saturday July 15, 2023 @09:55AM (#36736785)
I agree that, if you are working with checked exceptions, that’s certainly better than adding garbage to your interface.

But for many types of checked exception you just end up wrapping the underlying exception in a foo exception anyway.

I think you are right: if you are using exceptions for error handling, of errors you expect people to actually deal with, this is currently a reasonable (if painful) option to ensure exhaustive analysis of returns — which most people agree is a good thing these days I think, what with mainstream popularization of options etc.

Re:Why Checked Exceptions Failed by thewix on Saturday July 15, 2023 @08:59AM (#36736267)
Exceptions should absolutely be a part of an interface and it stops you from leaking implementation details. If I have an IRepository interface then none of the methods should ever throw SqlException, HttpException, or anything like that. You'd map them to something else. Exceptions in most languages are implicitly part of the contract, and not having consistent ones on an interface breaks Liskov's Substitution Principle.
Re:Why Checked Exceptions Failed by necovek on Saturday July 15, 2023 @12:55PM (#36738754)
As others have pointed out, my example would have been better if it said `ExampleExceptionFoo, ExampleExceptionBar` to more clearly indicate that an interface would define what exceptions it could throw as part of the contract. Individual implementations could extend those base exceptions if they need to.

There is not much difference between this and using error types, except the syntax sugar.