See the Tutorial List

Method Overriding with Exception Handling

There are few things to remember when overriding a method with exception handling. If super class method does not declare any exception, then sub class overriden method cannot declare checked exception but it can declare unchecked exceptions.


Method overriding in Subclass with Checked Exception

Checked exception is the exception which is expected or known to occur hence they must be handled at compile time.

import java.io.*;

class Super
{
  void show() { 
    System.out.println("parent class"); 
  }
}

public class Sub extends Super
{
  void show() throws IOException		//Compile time error
  { 
    System.out.println("parent class"); 
  }

  public static void main(String[] args)
  {
    Super s=new Sub();
    s.show();
  }
}

In the example above, the method show() doesn't throw any exception when its declared/defined in the Super class, hence its overridden version in the class Sub also cannot throw any checked exception.

If we try to do so, we will get a compile time error.


Method overriding in Subclass with UnChecked Exception

Unchecked Exceptions are the exception which extend the class RuntimeExeption and are thrown as a result of some runtime error.

import java.io.*;

class Super
{
  void show() { 
    System.out.println("parent class"); 
  }
}

class Sub extends Super
{
  void show() throws ArrayIndexOutOfBoundsException
  { 
    System.out.println("child class"); 
  }

  public static void main(String[] args)
  {
    Super s = new Sub();
    s.show();
  }
}

child class

Because ArrayIndexOutOfBoundsException is an unchecked exception hence, overrided show() method can throw it.


More about Overriden Methods and Exceptions

If Super class method throws an exception, then Subclass overriden method can throw the same exception or no exception, but must not throw parent exception of the exception thrown by Super class method.

It means, if Super class method throws object of NullPointerException class, then Subclass method can either throw same exception, or can throw no exception, but it can never throw object of Exception class (parent of NullPointerException class).


Example of Subclass overriden method with same Exception

import java.io.*;
class Super
{
 void show() throws Exception
  {  System.out.println("parent class");  }
}

public class Sub extends Super {
 void show() throws Exception		//Correct
   { System.out.println("child class"); }

 public static void main(String[] args)
 {
  try {
   Super s=new Sub();
   s.show();
   }
  catch(Exception e){}
 }
}

child class


Example of Subclass overriden method with no Exception

import java.io.*;
class Super
{
 void show() throws Exception
  {  System.out.println("parent class");  }
}

public class Sub extends Super {
 void show()             		//Correct
   { System.out.println("child class"); }

 public static void main(String[] args)
 {
  try {
   Super s=new Sub();
   s.show();
   }
  catch(Exception e){}
 }
}

child class


Example of Subclass overriden method with parent Exception

import java.io.*;
class Super
{
 void show() throws ArithmeticException
  {  System.out.println("parent class");  }
}

public class Sub extends Super {
 void show() throws Exception         		//Cmpile time Error
   { System.out.println("child class"); }

 public static void main(String[] args)
 {
  try {
   Super s=new Sub();
   s.show();
   }
  catch(Exception e){}
 }
}