Rules of Exceptions in Method Overriding

After knowing Rules of "Access Specifiers in Method Overriding", let us find the "Rules of Exceptions in Method Overriding".

The rule says "Subclass overridden method cannot throw more checked exceptions than that of super class method".

Let us explain the rule through a program for clear and easy understanding.

The following program does not compile as subclass display() method throws Exception, the super class of IOException (thrown by super class display method).

import java.io.*;
class Test
{
    public void display() throws IOException  {    }
}
public class Demo extends Test
{
    public void display() throws Exception  {   }  //  throwing super class exception
}

The above program does not compile as subclass display() method cannot throw more than IOException. That is, it cannot throw the superclasses of IOException. It can throw either the same (IOException) or its subclass FileNotFoundException.

Observe the compiler message.

Exceptions in Method Overriding

We know in exception hierarchy, that IOException super class is Exception and subclass is FileNotFoundException.

Following combinations of the above code works nice.

The following program works well as subclass display() method throws the same exception, IOException, thrown by super class display() method.

import java.io.*;
class Test
{
    public void display() throws IOException  {    }
}
public class Demo extends Test
{
    public void display() throws IOException  {   }   // throwing the same
}

The following program also works well as subclass display() method throws the subclass exception FileNotFoundException of IOException.

import java.io.*;
class Test
{
    public void display() throws IOException  {    }
}
public class Demo extends Test
{
    public void display() throws FileNotFoundException {   }   // throwing subclass exception
}

A novice should be aware of this rule while coding.

One stop destination for all Java Exception world.

Looks confusing, the following.

1. Do you know, a try block can be written with finally block without catch block?
2. Do you know, the three styles of handling exceptions.
3. Do you know, the rules imposed by access specifiers in method overriding?
4. Do you know, how to avoid try-catch even with checked exceptions?

12 thoughts on “Rules of Exceptions in Method Overriding”

  1. saikrishnareddy

    please explain about proiority of exceptions if 2 or more exceptions occur at same time (un checked exception )
    ex:-
    int a[]=new int [50];
    int b=0;
    a[150]=10/b;

      1. Mohd Irshad SHEIKH

        sir but there will be problem at the time of method invocation,eg if we take
        super class reference subclass object into consideration, than at the time
        overriden method invocation there will be compilation error eg

        class Animal
        {
        public void eat() throws Exception
        {
        //throws exception
        }

        }

        class Dog extends Animal
        {
        public void eat()
        {
        /*no exception*/
        }

        }
        public static void main(String[] args)
        {
        Animal a=new Animal();
        Dog d=new Dog();
        d.eat();//class Animal
        {
        public void eat() throws Exception
        {
        //throws exception
        } //compiler error

        }
        a.eat();//
        }

  2. sir, pls explain that “outer catch() can handle the exception in case of inner catch fails to handle the exception with an example”

    1. try
      {
      try
      {
      int c = 10/0;
      }
      catch(ArrayIndexOutOfBoundsException e)
      {
      System.out.println(“some problem 1.” + e);
      }
      }
      catch(ArithmeticException e)
      {

      System.out.println(“some problem 2.” + e);
      }

      In the above code, the inner catch fails to handles the exception thrown by the inner try block. The outer catch can handle the exception.

Leave a Comment

Your email address will not be published.