VisionSet said:
This is related to a previous post, the one that ended in a virtual punch up
- "Overriden method doesn't throw Exception, super may."
When you design an interface (and resulting classes) where exceptions may be
thrown how do you deal with the situation where different implementations
may or may not throw those exceptions.
If an exception has a logical meaning in the context of an interface
method, you declare it, along with documentation clarifying exactly when
it is thrown (though the general idea should be obvious). If your
client knows that they'll be using a specific implementation of the
interface that doesn't throw that exception, then they won't need to
worry about it anyway.
If an exception might be thrown by implementing code but *doesn't* make
sense from the perspective of someone using the interface, then you
write your own checked exception class that does make sense in that
context, and then rely on the implementor to nest their exception into
your own checked exception. For example, see the custom exception class
javax.mail.MessagingException and its uses in JavaMail. Declaring
IOException is a little too low-level for the JavaMail API, so an
IOException in the implementation of a mailbox will end up wrapped in
MessagingException before being thrown to a client.
I'm not considering the case where you might want to throw different
exceptions.
Okay, but that last bit also works for arbitrary exceptions in the
implementation, should the situation come up.
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation