Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever
by nradov on Thursday July 13, 2023 @07:00PM
(#36717262)
The trouble with checked exceptions is that they prevent you from easily extending classes or implementing interfaces that you don't control. Your new class might need to throw a checked exception not included in the method signature. So then you have to resort to hacks like wrapping the new checked exception inside a runtime exception.
[ Parent
| on HN ]
Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever
by breadwinner on Thursday July 13, 2023 @09:28PM
(#36718350)
Not true. Exception hierarchies solve this problem.
Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever
by nradov on Thursday July 13, 2023 @09:32PM
(#36718373)
Nope. Exception hierarchies don't even come close to solving that problem because the superclass or interface author usually didn't anticipate your need and define the method signature in a way that would be useful with exception hierarchies.
Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever
by breadwinner on Thursday July 13, 2023 @09:58PM
(#36718546)
So you're inheriting a class not within your own application, but written outside of your application. Composition might work better in this case.
Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever
by nradov on Thursday July 13, 2023 @10:09PM
(#36718613)
Nope. Composition doesn't help when you have to implement an existing interface in order to make some API work.
Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever
by int_19h on Thursday July 13, 2023 @10:38PM
(#36718802)
If you're implementing an existing interface for that reason, it's because something else is going to be calling you. In which case throwing an exception that they don't expect is probably a bad idea, and relying on it just flowing through their code back to you is basically relying on implementation details in many cases.
Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever
by nradov on Thursday July 13, 2023 @11:19PM
(#36719096)
No, that's not how it usually works in practice. Typically where this becomes a problem is in using an existing library as a middle layer in your application code. So you could catch your own additional exceptions, but the interface method signatures don't allow for that. Hence the need for ugly hacks like wrapping the checked exception in a runtime exception, then catching that and extracting the original exception. A real mess, but still better than the alternative of forking and modifying the library.
Re:Unchecked Java: Say Goodbye to Checked Exceptions Forever
by int_19h on Saturday July 15, 2023 @03:46PM
(#36740291)
That is exactly my point - when you have a middle layer written by someone else sandwiched between parts of your code, assuming that any random exception you can throw will flow through (or, for that matter, that there even is a "through" in many cases - e.g. if the library makes some part async) - is relying on implementation details of said code.