4 ms·
Checked Exceptions. I wish Kotlin had it too.
by ashishb 5y ago
Checked Exceptions. I wish Kotlin had it too.
- kubb 5y agoThey don't have it by design. The Java community has collectively decided it's a bad idea and they're discouraged even in Java.
- inshadows 5y agoNo it didn't. And they're not discouraged in Java SE. Ignoring errors and letting them propagate to thread's main is just stupid. I haven't heard single argument except "Meh, I have to write throws Exception to every method". It's either "throws Exception" or something like "Either<Exception, String>". Same principle, you declare errors in type system. And if you have reason to believe that it's a good thing to hide errors from type system, then please, convince me.
- blacktriangle 5y agoChecked exceptions sound good in theory but to make it works it requires the whole community to buy into that level of paranoia right from the beginning. It's a much better concept for a language like Ada or Eiffel more design-by-contract style. Java already plays fast and lose with safety, checked exceptions are closing the barn door after the horses bolted. In reality checked exceptions in Java just means everybody swallows exceptions by doing nothing and moves on.
- ashishb 5y ago> It's a much better concept for a language like Ada or Eiffel more design-by-contract style. I'm not familiar with this. Please elaborate how you would handle say an I/O failure while R/w a file with this approach?
- blacktriangle 5y agoI'm not familiar enough with either of those platforms to answer that specific question, it was more a general comment on the respective developer cultures. I've worked on several Java codebases where swallowing exceptions was the norm, but in their defense it was because several libraries were way too chatty in their use of exceptions.
- ashishb 5y agoI worked on several Java codebases where more than 90% exceptions weren't swallowed. The other 10% were swallowed with a comment explaining why it is fine to ignore.
- specialist 5y agoEmphatic agreement. Unchecked exceptions are preferred because many frameworks (APIs) are poorly designed. The Correct Answer[tm] is to fix those libraries, not paper over their flaws.