[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Java exceptions (was Re: New Lisp ?)



Andreas Bogk <andreas@andreas.org> writes:
> Jeffrey Siegal <jbs@quiotix.com> writes:
> 
> > What happens in Java is that you have to at least declare the exceptions
> > up the chain anyway (the compiler will reject a method that doesn't
> 
> Yes, and that bothers me to no end.  I want to have specific code that
> knows about an exception in exactly two places: where it is generated,
> and where it can be handled.  All the code inbetween doesn't need to
> know more than that an operation has failed and that it needs to clean
> up.

Well you actually do have the choice in Java. Just have your exceptions extend
from RuntimeException or Error, and they are no longer required to be declared
in the throws clauses.

Then you have the knowledge of the exception to be exactly in the desired
places.

To "hide" the fact that you are possibly abusing the notion of "error", have a
base application exception class extend from RuntimeException or Error, and
have all you other exceptions extend from that.

Personally, though, I prefer having the explicit throws clauses, for then the
compiler forces me to be aware of the error issues. At the very least I know to
put in the necessary "finally" blocks and rethrow if handling the error is not
appropriate.

I have not found the maintenance issue to be too bad. One trick is to rethrow
in terms of a more general exception class, so that the methods in between the
low level error and the final handler just have one or two exception classes in
their throws clauses, as opposed to a miriad of particular ones (which cause
the maintenance problem).

The mistake that a lot of Java programmers make is to simply swallow the
unexpected exceptions with a stack trace and continue. This is the worst of all
possibilities, defeating the purpose of exceptions in the first place.

-- 
Cheers,                                        The Rhythm is around me,
                                               The Rhythm has control.
Ray Blaak                                      The Rhythm is inside me,
blaak@telus.net                                The Rhythm has my soul.