Java | Method overriding and exceptions

|
| By Webner

Problem: Suppose you have a method M1 in class A that throws exception E1 and you override this method in class B (B extends A). Are there any rules about exceptions when methods are overridden?

Solution:

There are following rules for overriding method if it is throwing exceptions:

If a superclass declares an exception then following are the key factors about exceptions:

1. Subclass overriding method cannot declare parent exception of the superclass exception.
2. Subclass overriding method can declare any unchecked exception no matter what the superclass exception is.
3. Subclass overriding method can declare no exception.
4. Subclass overriding method can declare subclass exception of the parent exception .
5. Subclass overriding method can declare same exception.

1.

Subclass overriding method cannot declare parent exception of the superclass exception
class ParentClass {
    public void printData() throws IndexOutOfBoundsException {
        String s[] = new String[2];
        s[3] = "30";
        System.out.println("Super.test()");
        System.out.println(s[3]);

    }
}
class ChildClass extends ParentClass {
    @Override
    public void printData() throws Exception {
        System.out.println("Sub.test()");
    }

Output: compile time error – Exception is not compatible with the throws clause of the ParentClass.printData()

2.

Subclass overriding method can declare any unchecked exception no matter what the superclass exception is.
class ParentClass {
    public void printData() throws IndexOutOfBoundsException {
        String s[] = new String[2];
        s[3] = "30";
        System.out.println(s[3]);
        System.out.println("Super.test()");
    }
}

class ChildClass extends ParentClass {
    @Override
    public void printData() {
        int c = 5 / 0; //this line will throw runtime exception because number 5 is divided by zero 
        //which is invalid operation
        System.out.println("Sub.test()");
    }
}

Output : In the sub class it will throw Arithmetic exception which is unchecked exception. It is valid for a subclass overridden method to throw unchecked exceptions. So when we make object of subclass and call its print Data method, it will throw Arithmetic exception at run time.

3.

Subclass overriding method can declare no exception.
class ParentClass {
    public void printData() throws Exception {
        System.out.println("Super.test()");
    }
}

class ChildClass extends ParentClass {

    @Override
    public void printData() {
        String s[] = new String[2];
        s[3] = "30"; // ArrayIndexOutOfBoundsException  which is unchecked exception
        System.out.println(s[3]);
        System.out.println("Sub.test()");
    }
}

Output: It will throw exception at run time as below:
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 3
at model.ChildClass.printData(ExceptionOverriding.java:22)
at model.ExceptionOverriding.main(ExceptionOverriding.java:7)

4.

Subclass overriding method can declare subclass exception of the parent exception.

class ParentClass {
    public void printData() throws Exception {
        System.out.println("Super.test()");
    }
}

class ChildClass extends ParentClass {

    @Override
    public void printData() throws ArrayIndexOutOfBoundsException {
        String s[] = new String[2];
        s[3] = "30";
        System.out.println(s[3]);
        System.out.println("Sub.test()");
    }
}

5.

Subclass overriding method can declare same exception.
class ParentClass {
    public void printData() throws ArrayIndexOutOfBoundsException {
        System.out.println("Super.test()");
    }
}

class ChildClass extends ParentClass {

    @Override
    public void printData() throws ArrayIndexOutOfBoundsException {
        String s[] = new String[2];
        s[3] = "30";
        System.out.println(s[3]);
        System.out.println("Sub.test()");
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *