|
Re: Failure and Exceptions
|
Posted: Sep 22, 2003 4:54 AM
|
|
It is true that mixing return values and "exception" values in the same return value is bad practice - but checked exceptions are not the only necessary solution. A more sane API could do the same job. For example, if you have a FileDescriptor object, "read()" should work normally while there are bytes remaining and should throw an EOF exception if there aren't. There should be another method "isEOF()" that enables the programmer to check for EOF.
For the "does a file exist" case, there should also be an "exists()" method (as there is in Java). However, it's a bit verbose if there's a checked exception as well, because you check the file exists with the method call AND then you must still handle the exception anyway.
Also, forcing someone to handle an exception, does not force them to handle an exception well - this is perhaps my biggest problem with checked exceptions. I have seen many cases where the exception is simply swallowed and nothing indicates there is a problem. Or alternatively, a new exception is throw but the initial exception is discarded so it's stack trace is unavailable (and this is sometimes in a part of code you may not have the source to change). I'd rather have an unchecked exception that makes everthing grind to a halt with a sensible stack trace rather than either of these cases.
As for having a centralised handling of Exceptions, this is one area where AOP holds promise. It makes sense to centralise the handling because it's often the same kind of problem you want to handle, eg RemoteExceptions. However, as James says, sometimes local information is useful too - but with AOP, this information would be available even if the handling were centralised. Unfortunately, Java will continue to force you to put handling code in locally even if handling them with AOP becomes widespread.
Finally, something in favour of checked exceptions: in TDD, conditions that raise exceptions should obviously be tested. Having checked exceptions is nice for this because they point this out.
Richard
|
|