Re:Java’s Checked Exceptions Are Evil? (2015)
by rwmj on Sunday September 23, 2018 @07:13AM
(#18050532)
The trouble is that in most programs I want to catch most exceptions very near the top ("main" function), not in the calling function immediately above where the exception is thrown. In other words the check is useless and done in the wrong place.
Of course there are exceptions which are really return values that do need to be checked by the calling function, but that's a misuse of exceptions in languages which don't support lightweight qualified unions (such as those provided in ML/Haskell).
In any case I think Erlang's quite different approach to handling errors is far superior.
Re:Java’s Checked Exceptions Are Evil? (2015)
by simongray on Sunday September 23, 2018 @09:00AM
(#18050896)
> The trouble is that in most programs I want to catch most exceptions very near the top ("main" function), not in the calling function immediately above where the exception is thrown. In other words the check is useless and done in the wrong place
It's been a while since I've done any Java, but I seem to remember that you can just annotate your function with the "throws" keyword to propagate exceptions up the call stack, so that you can handle them somewhere else.
Re:Java’s Checked Exceptions Are Evil? (2015)
by smsm42 on Wednesday September 26, 2018 @02:37PM
(#18078964)
Yes you can, but the problem is that this means reannotating all the stack up to "main" every time some tiny implementation detail changes - or you use different JSON parser that has "JsonParsingException" and not "JsonParserException", and so on. Or, you have to wrap them all into generic/runtime exceptions, which negates the whole benefit.
Re:Java’s Checked Exceptions Are Evil? (2015)
by rwmj on Sunday September 23, 2018 @09:02AM
(#18050899)
It's also been a long time for me, but I believe you're correct that annotating functions with "throws Exception" makes the function unchecked. However the problem is still twofold: (1) You have to change the called function. (2) The default is wrong.
Checked exceptions require reannotating the call graph on a change, but that's not surprising: you would have yo do the same thing for changes in result types. Failure modes are no less part of the interface. For generic code you need to be able to abstract over exception types, it seems that Java lacks the capability.
An escape hatch to dynamic unannotated exceptions would still be useful: C++ will mantain that.