3 ms·
I think the authors has some good points, but I don't think the proposed solutions really solve the underlying problem. In particular: > But how do I know, th
by xg15 8y ago
I think the authors has some good points, but I don't think the proposed solutions really solve the underlying problem.
In particular:
> But how do I know, that a method throws an unchecked exception? The compiler doesn’t tell me. Yes, you are responsible to document unchecked exceptions carefully.
Except if you try that, the same problem that makes checked exception problematic will bite you here too - except now it's easier to blame yourself than blaming the compiler.
If you write
void foo() throws FooException
all you can really say is "It might be useful to consider FooExceptions when calling this function".
- You can't guarantee to a caller that FooException is always important to consider - because, as the author correctly stated, there might be situations where you call foo() but it's formally impossible that a FooException is raised.
- You can't guarantee that FooException is the only important exception to consider either, because foo() might call other functions that might raise different exceptions on their own - a fact that you could probably read up in those other functions' documentations, if you knew you had to deal with those other functions in the first place.
This limitation stays whether FooException is checked or not.
In then end, what you'd like to know when calling a function is the complete set of exceptions this call site could produce at runtime. Checked exceptions were a an attempt to answer this question, even if it was a very clumsy and eventually unworkable one.
Maybe a better solution would be more sophisticated static analysis, which could try to answer this question as part of an IDE. However, this would require that the analysis tool get complete information about all abstract functions calls, interceptors and other things that could happen as part of a call. This seems somewhat doable with fully static languages like Go but seems to run completely against Java's philosophy of being extensible at runtime.