 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Wed Sep 20, 2006 12:37 am Post subject: Throw exception in an interface |
|
|
Hello,
I defined a basic interface like this:
public interface Inter {
public void method();
}
Now I implemented it like this:
public class InterImpl implements Inter {
public void method() throws Exception {
// Do some stuff that can throw exceptions
}
}
It doesn't compile because the two signatures are not the same in the
interface and the implementation. To me, the interface should be
independant from the implementation, that's why I didn't but a throws
in the interface definition. But it's likely that when you implement
it, some exceptions are thrown. How would design this problem?
Sydney |
|
| Back to top |
|
 |
Tor Iver Wilhelmsen Guest
|
Posted: Wed Sep 20, 2006 2:36 am Post subject: Re: Throw exception in an interface |
|
|
moisi_fr (AT) yahoo (DOT) fr writes:
| Quote: | To me, the interface should be independant from the implementation,
that's why I didn't but a throws in the interface definition. But
it's likely that when you implement it, some exceptions are thrown.
How would design this problem?
|
Basically the requirement that a subclass B of A cannot add exceptions
to a overridden method from A (since then it cannot function as an A)
prevents classes implementing interfaces from adding them either since
the interface is a super*type* of the class' *type*.
So: The declared checked exception is not part of the implementation,
but part of the method's "contract". |
|
| Back to top |
|
 |
opalpa@gmail.com opalinsk Guest
|
Posted: Wed Sep 20, 2006 7:10 am Post subject: Re: Throw exception in an interface |
|
|
moisi_fr (AT) yahoo (DOT) fr wrote:
| Quote: | Hello,
I defined a basic interface like this:
public interface Inter {
public void method();
}
Now I implemented it like this:
public class InterImpl implements Inter {
public void method() throws Exception {
// Do some stuff that can throw exceptions
}
}
It doesn't compile because the two signatures are not the same in the
interface and the implementation. To me, the interface should be
independant from the implementation, that's why I didn't but a throws
in the interface definition. But it's likely that when you implement
it, some exceptions are thrown. How would design this problem?
Sydney
|
Make your own Exception sublass that is broad enough to suitcase all
exceptions. Then declare the interface method as throwing that
SuitcaseException. As case study: a) RMI interfaces all declare the
nebulous RemoteException, and b) sometimes methods declare IOException
only to throw FileNotFoundException instnaces.
Another, occasionally appropriate solution is to throw RuntimeException
instances like IllegalArgumentException and those subclasses of
RuntimeException you roll yourself.
Opalinski
opalpa (AT) gmail (DOT) com
http://www.geocities.com/opalpaweb/ |
|
| Back to top |
|
 |
Lew Guest
|
Posted: Thu Sep 21, 2006 7:11 am Post subject: Re: Throw exception in an interface |
|
|
| Quote: | moisi_fr (AT) yahoo (DOT) fr wrote:
Hello,
I defined a basic interface like this:
public interface Inter {
public void method();
}
Now I implemented it like this:
public class InterImpl implements Inter {
public void method() throws Exception {
// Do some stuff that can throw exceptions
}
}
It doesn't compile because the two signatures are not the same in the
interface and the implementation. To me, the interface should be
|
opalpa (AT) gmail (DOT) com opalinski from opalpaweb wrote:
| Quote: |
Make your own Exception subclass that is broad enough to suitcase all
exceptions. Then declare the interface method as throwing that
SuitcaseException.
snip
Another, occasionally appropriate solution is to throw RuntimeException
instances like IllegalArgumentException and those subclasses of
RuntimeException you roll yourself.
Opalinski
|
The trick is knowing if this time is such an appropriate occasion.
RuntimeExceptions are dangerous because they are not in the method signature,
so the client can be pretty surprised by them. Like many other dangerous
things, they can be useful if correctly used to handle wildly exceptional
situations. They're not so good for handling stuff the method rather expects
to happen sometimes.
If you intend your interface to throw Exceptions, it's better to declare a
checked exception such as the respondent's "SuitcaseException" in the
interface as they suggested. This warns all clients, in fact forces them to
check for the exception. Another alternative is not to throw an exception but
to have the method take some prudent alternative action.
-Lew |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|