4 ms·
Make the signature of your generic callback "throws Throwable". It's generic; it should never care about the specific types that the callback can throw. (Exce
by AnimalMuppet 2y ago
Make the signature of your generic callback "throws Throwable". It's generic; it should never care about the specific types that the callback can throw.
(Except that then you have to decide what your generic function is going to do if the callback throws an exception...)
- lmm 2y ago> Make the signature of your generic callback "throws Throwable". It's generic; it should never care about the specific types that the callback can throw. > (Except that then you have to decide what your generic function is going to do if the callback throws an exception...) Exactly. Presumably you don't want to handle them and want to throw them up to the caller. But now your function has to be "throws Throwable" rather than throwing the specific exception types that the callback throws.
- bluGill 2y agoBy doing that you lose all the benefits of checked exceptions. If you have checked exceptions everywhere the compiler will tell you when an exception is not handled and in turn you can ensure you handle it. Of course in general the manual effort to do that in a large code base ends up too hard and so in the real world nobody does that. Still the ideal is good, just the implementation is flawed.
- throwaway-9111 2y agoIn that case you can use the lombok @SneakyThrows annotation that converts a checked exception to a runtime exception