003 Java OOPS - Exception handling in Method overriding with example
Posted by Superadmin on February 27 2017 06:13:00

Exception handling in Method overriding with example

BY CHAITANYA SINGH | FILED UNDER: OOPS CONCEPT

In the last post we discussed about method overriding. In this post we will see how to do exception handling for overriding and overridden methods.

Rule:  An overriding method (the method of child class) can throw any unchecked exceptions, regardless of whether the overridden method (method of base class) throws exceptions or not. However the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method. The overriding method can throw those checked exceptions, which have less scope than the exception(s) declared in the overridden method.

Let’s understand the above explanation with the help of few examples:

Example 1: If base class doesn’t throw any exception but child class throws an unchecked exception.
In this example class Room is overriding the method color(). The overridden method is not throwing any exception however the overriding method is throwing an unchecked exception (NullPointerException). Upon compilation code ran successfully.

class Building {  
   void color()
   {
       System.out.println("Blue");
   }  
}
class Room extends Building{
   //It throws an unchecked exception
   void color() throws NullPointerException
   {
       System.out.println("White");
   }  
   public static void main(String args[]){  
       Building obj = new Room();  
       obj.color(); 
   } 
}

Output:

White

Example 2: If base class doesn’t throw any exception but child class throws an checked exception

import java.io.*;
class Building {  
   void color()
   {
      System.out.println("Blue");
   }  
}
class Room extends Building{
   void color() throws IOException
   {
      System.out.println("White");
   }  
   public static void main(String args[]){  
      Building obj = new Room();  
      try{
         obj.color();
      }catch(Exception e){
         System.out.println(e);
       }
   } 
}

Output:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Exception IOException is not compatible with throws clause in Building.color()

The above code is having a compilation error: Because the overriding method (child class method) cannot throw a checked exception if the overridden method(method of base class) is not throwing an exception.

Example 3: When base class and child class both throws a checked exception

import java.io.*;
class Building {  
   void color() throws IOException
   {
       System.out.println("Blue");
   }  
}
class Room extends Building{
    void color() throws IOException
    {
        System.out.println("White");
    }  
    public static void main(String args[]){  
        Building obj = new Room();  
        try{
	   obj.color();
	}catch(Exception e){
	   System.out.println(e);
	 }
    } 
}

Output:

White

The code ran fine because color() method of child class is NOT throwing a checked exception with scope broader than the exception declared by color() method of base class.

Example 4: When child class method is throwing border checked exception compared to the same method of base class

package beginnersbook.com;
import java.io.*;
class Building {  
	void color() throws IOException
	{
		  System.out.println("Blue");
	}  
}
class Room extends Building{
	  void color() throws Exception
	  {
		  System.out.println("White");
	  }  
	  public static void main(String args[]){  
		   Building obj = new Room();  
		   try{
		   obj.color();
		   }catch(Exception e){
			   System.out.println(e);
		   }
	  } 
}

Output:
Compilation error because the color() method of child class is throwing Exception which has a broader scope than the exception thrown by method color() of parent class.