Method signature declared as throws Exception; implemented as throws a subclass of Exception
I have the following interface declaration:
public interface SomeInterface {
开发者_运维问答void someMethod() throws Exception;
}
I use a third party to generate an implementation of this class (JavaCC - for the curious)
The generated class looks naively like this:
public class SomeClass implements SomeInterface {
public void someMethod() throws SomeException {
// Does something
}
}
Where SomeException
is of course a subclass of Exception
.
(Not) surprisingly the code does not compile.
Does anyone have any input concerning this?
Thanks!
EDIT:
renamed the method SomeMethod()
to someMethod()
.
It had been a typo of mine... (sorry)
EDIT #2:
Sorry all - huge mistake of mine. Writing this example had forced me to strip down the code. I had not noticed that the mistake was elsewhere and not with the signature.
Thats the "magic" of runtime compile and custom class loading...
It doesn't compile because the method names aren't the same (check the caps on S/someMethod)
Case is important in Java. Your interface says someMethod
and your class says SomeMethod
.
Why does your interface method throw Exception? This is almost always wrong. Exception is just the base type the rest extend from; it's not meant to be used this way.
One of the methods protection is public and the other is default, that's why your code does not compile. Make both public or default.
精彩评论