Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Throwing exceptions on an extended class?

807601Apr 21 2008 — edited Apr 21 2008
I currently have a class that I need to extend and one method of that class that I need to override.

Assuming the following:
public BaseClass {}
public BarClass extends BaseClass {
    public configure () {}
}
public FooClass extends BarClass {
    @Override
    public configure () {
    }
}
I want to throw custom exceptions from FooClass, but haven't as yet found the magic behind doing that. If I add a throws statement to FooClass, eg.
public FooClass extends BarClass throws MyException{
    @Override
    public configure () {
        ...
        throw new MyException();
    }
}
javac complains that a "{" was expected on the FooClass declaration.

If I add the throws to the configure() method in FooClass, javac complains that the overridden class does not throw MyException. eg.
public FooClass extends BarClass{
    @Override
    public configure ()  throws MyException {
        ...
        throw new MyException();
    }
}
First, should I be able to throw my own exceptions on overridden methods of an extended class? If yes, what am I missing?

-David
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 19 2008
Added on Apr 21 2008
9 comments
2,207 views