3 ms·
> Also, other than Swift, I don't know of any programming language that let's me explicitly mention if a function could throw an exception. Checked exceptions
by flakes 2y ago
> Also, other than Swift, I don't know of any programming language that let's me explicitly mention if a function could throw an exception.
Checked exceptions in Java. I both like and hate them.
They force the function to be explicit about what exceptions they can raise. If you call a function that raises a checked exception, you must either explicitly handle the exception, or mark that it is propagated up by adding the same exception to the calling method signature.
However, they cause a lot of pain for working with higher order methods (map, filter, flatmap, etc). Because they change the signature of the method (and therefore the interface it can satisfy), you need to use a lot of generic variants of higher order methods to accept them, or like a lot of libraries, you end up writing wrapper functions that convert the checked exceptions into unchecked runtime exceptions, such that they don't modify the method signatures. This then leads to a lot of weird code, and uncaught exceptions at runtime, taking down the application.
- int_19h 2y agoThe problem here isn't checked exceptions as such, it's that they are not first-class, so you can't write generic code in terms like "I take F and throw all E that F throws plus X". Back when Java was adding lambdas, one of the competing proposals actually tried to fill that gap by having generic exception specification parameters: https://tronicek.blogspot.com/2007/12/exceptions.html https://tronicek.blogspot.com/2007/12/exceptions.html: static <throws E> void performTwice({ => void throws E } block) throws E { block.invoke(); block.invoke(); } Alas, it was not to be.