0
package com.rnd.core.java;


import java.io.IOException;
public class TestExceptionInheritance {

    public void checkExcpetions () throws ArrayIndexOutOfBoundsException{
        System.out.println("Inside TestExceptionInheritance ParentClass");
        throw new ArrayIndexOutOfBoundsException();

    }

}

    package com.rnd.core.java;


import javax.sound.midi.MidiUnavailableException;
public class TestExceptionInheritance2 extends TestExceptionInheritance {

    public void checkException () throws MidiUnavailableException {
        System.out.println("Hello");
        throw new MidiUnavailableException();
    }


    @Override
    public void checkExcpetions() throws StringIndexOutOfBoundsException {
        // TODO Auto-generated method stub
        //super.checkExcpetions();
        System.out.println("HI");
    }


    public static void main(String[] args) throws Exception  {
        TestExceptionInheritance obj = new TestExceptionInheritance2();
        obj.checkExcpetions();

    }
}

I have overriden the checkException Method of the parent class in my subclass but I throw a different exception here.

I want to understand why the compiler allows me to throw an altogether different exception; though I know that the method version would be decided based on the reference type.

-------------------Edit 1---------------------------

I have added an @override notation over the overridden method. Overridden method allows me to throw StringIndexOutOfBoundException and RunTimeException along with ArrayIndexOutOfBoundException but not any other exception like for example Exception.

According to the Exception class hierarchy, both StringIndexOutOfBoundException and ArrayIndexOutOfBoundException are subclasses of IndexOutOfBoundException.

How and why does the compiler allows me to throw StringIndexOutOfBoundException because ArrayIndexOutOfBoundException will be never be caught in StringIndexOutOfBoundException.

Thanks for your help.

2
  • 2
    You can always throw unchecked exceptions. Try with checked exceptions. Commented May 17, 2014 at 1:23
  • @chrylis, +1. From what I have tested it points out that exception in the overrridden method must be same or a subclass of the exception thrown in the superclass method. Commented May 17, 2014 at 2:27

1 Answer 1

3

The real simple answer is you are not overriding what you think you are. The parent class declares a function public void checkExcpetions () and you have a function public void checkException (). These are two different functions which is why there is no compiler error

Using the @Override tag is one way to have the compiler check that you are overriding what you think you are. In this case if you used the tag there would be an error since you are not overriding a parent method

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for the annotation. I have update my question for some more queries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.