Exception handling with Method overriding

Here some rules of Exception handling with Method overriding. If you are overriding the method in child class then you should take care of these scenarios. If you are not familiar with the exception handling and method overriding, then you should read them first. Because these will help you to understand the rules here.

Scenario 1: If parent class doesn’t throw any exception but child class throws an unchecked exception

In this example, we have Parent class and child class having a method color(). The method of Parent class is not throwing any exception. However, the overriding method of Child class is throwing an unchecked exception (NullPointerException). This will run successfully.

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

Output: White

Scenario 2:  If parent class doesn’t throw any exception but child class throws a checked exception

In this example, we have Parent class and child class having a method color(). The method of Parent class is not throwing any exception. However, the overriding method of Child class is throwing a checked exception (IOException). This will compilation error at compile time successfully. Because the overriding method (Child class method) can’t throw a checked exception if the overridden method(Parent class method) is not throwing an exception.

import java.io.IOException;

class ParentClass 
{  
   void color()
   {
       System.out.println("Red");
   }  
}
class ChildClass extends ParentClass {
   //It throws a checked exception
   void color() throws IOException
   {
       System.out.println("White");
   }  
   public static void main(String args[]){  
	   ParentClass obj = new ChildClass();  
       obj.color(); 
   } 
}

Output: Exception in thread “main” java.lang.Error: Unresolved compilation problem: Exception IOException is not compatible with throws clause in ParentClass.color() at ChildClass.color(ChildClass.java:12) at ChildClass.main(ChildClass.java:18)

Scenario 3: If parent class and child class both throws a checked exception

import java.io.IOException;

class ParentClass 
{  
   void color() throws IOException
   {
       System.out.println("Red");
   }  
}
class ChildClass extends ParentClass {
   //It throws a checked exception
   void color() throws IOException
   {
       System.out.println("White");
   }  
   public static void main(String args[]){  
	   ParentClass obj = new ChildClass();  
       try 
       {
		obj. color();
       } 
       catch (IOException e) 
       {
		e.printStackTrace();
       } 
   } 
}

The code will run fine because the color() method of Parent class and child class is throwing a checked exception and thrown an exception by child class method should not superclass of exception thrown by Parent class.

Scenario 4: If the overriding method in a subclass throws its superclass exception

import java.io.IOException;

class ParentClass 
{  
   void color() throws IOException
   {
       System.out.println("Red");
   }  
}
class ChildClass extends ParentClass {
   //It throws a checked exception
   void color() throws Exception
   {
       System.out.println("White");
   }  
   public static void main(String args[]){  
	   ParentClass obj = new ChildClass();  
       try 
       {
		obj. color();
       } 
       catch (Exception e) 
       {
		e.printStackTrace();
       } 
   } 
}

Output:
Compilation error because the color() method of child class is throwing Exception which is super class of thrown by method color() of parent class.

Leave a Comment