Exception Handling In Method Overriding

Exceptions are the customary way in java to a calling method that an abnormal condition has occurred. When a method encounters an abnormal condition that it can’t handle itself, it may throw an exception. Throwing an exception is like throwing or flashing red ball to indicate that there is a problem that can’t be handled where it occurred. Exceptions are caught by handlers which are positioned with the thread’s method invocation stack. If the calling method isn’t prepared to catch the exception, it throws the exception on its calling method and so on. In this article we are going to discuss about method overriding with exception handling in detail.

There are few points to remember when overriding a method with exception handling.

  • If base class method throws an exception, then child class overridden method can throw the same, child class exception or no exception, but it does not throw parent exception that is thrown by base class method.
  • If the base class method does not throw an exception, then child class overridden method cannot throw the checked exception but it can throw unchecked exception.

Program Examples of  Exception Handling in Method Overriding

Program 1: This is a program of subclass overridden method declaring Checked Exception.

Step 1: First we create a class Parent in which we declare method display():

import java.io.*;

class Parent

{

   void display()

   {

     System.out.println("parent class called");

   }

}

Step 2: Second we create a class Child which extends the class Parent in which we try to override the superclass method which throws IO Exception:

class Child extends Parent

{

   void display() throws IOException    //shows compile time error

   {

     System.out.println("child class called");

   }

   public static void main( String[] args )

   {

     Parent p=new Child();

     p.display();

   }

}

Output:

Error: display() in child cannot override display() in Parent
Overridden method does not throw IOException

Program 2: This is a program of subclass overridden method declaring Unchecked Exception.

Step 1: First we create a class Parent in which we declare method display():

import java.io.*;

class Parent

{

   void display()

   {

     System.out.println("parent class called");

   }

}

Step 2: Second we create a class Child which extends the class Parent in which we try to override the superclass method which throws ArrayIndexOutOfBoundsException:

class Child extends Parent

{

   void display() throws ArrayIndexOutOfBoundsException

  {

    System.out.println("child class called");

  }

  public static void main(String[] args)

  {

    Parent p=new Child();

    p.display();

  }

}

Output :

child class called

Explanation:

In this program, the unchecked exception is ArrayIndexOutOfBoundsException. Therefore, overrided display() method can throw it.


Program 3: This is a program of subclass overridden method with same Exception.

Step 1: First we create a class Parent in which we declare method display() which throws an exception:

import java.io.*;

class Parent

{

   void display() throws Exception

  {

    System.out.println("parent class called");

  }

}

Step 2: Second we create a class Child which extends class Parent in which we try to override the superclass method which throws Exception:

class Child extends Parent

{

  void display() throws Exception

  {

    System.out.println("child class called");

  }

  public static void main(String[] args)

  {

    try {

    Parent p=new Child();

    p.display();

    }

    catch(Exception e){}

  }

}

Output:

child class called

Program 4: This is a program of subclass overridden method with no Exception.

Step 1: First we create a class Parent in which we declare method display() which throws exception:

import java.io.*;

class Parent

{

  void display() throws Exception

  {

    System.out.println("parent class called");

  }

}

Step 2: Second we create a class Child which extends class Parent in which we try to override the superclass method:

class Child extends Parent

{

  void display()

  {

    System.out.println("child class called");

  }

  public static void main(String[] args)

  {

    try {

    Parent p=new Child();

    p.display();

    }

    catch(Exception e){}

  }

}

Output:

child class called

Program 5: This is a program of subclass overridden method with parent Exception.

Step 1: First we create a class Parent in which we declare method display() which throws ArithmeticException:

import java.io.*;

class Parent

{

  void display() throws ArithmeticException

  {

    System.out.println("parent class called");

  }

}

Step 2: Second we create a class Child which extends class Parent in which we try to override the superclass method which throws exception:

class Child extends Parent

{

  void display() throws Exception

  {

    System.out.println("child class called");

  }

  public static void main(String[] args)

  {

    try {

    Parent p=new Child();

    s.display();

    }

    catch(Exception e){}

  }

}

Output:

Error: display() in child cannot override display() in Parent
Overridden method does not throw Exception