4 ms·
As a counter point people have been trying to get the equivalent of checked exception in C++ for a while and there are well supported proposals on the table rig
by gpderetta 8y ago
As a counter point people have been trying to get the equivalent of checked exception in C++ for a while and there are well supported proposals on the table right now.
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.
- rwmj 8y agoThe 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.
- simongray 8y ago> 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.
- rwmj 8y agoIt'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.
- smsm42 8y agoYes 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.